对不起,我正在更新问题。
我正在编写一个接收以下格式输入的应用程序:
someId = 00000-000-0000-000000; someotherId = 123456789; someIdentifier = 3030;
有什么办法可以将这些值添加到通用LIST<T>
中,以便我的列表包含以下内容?
record.someid= 00000-000-0000-000000
record.someotherId =123456789
record.someIdentifier = 3030
很抱歉,我是新手,所以问这个问题。
最佳答案
您可以使用Split
来获取字符串的某些部分,这些部分似乎是key / value pair
组合,然后将键和值对添加到Dictionary
。
string str = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030";
string [] arr = str.Split(';');
Dictionary<string, string> dic = new Dictionary<string, string>();
for(int i=0; i < arr.Length; i++)
{
string []arrItem = arr[i].Split('=');
dic.Add(arrItem[0], arrItem[1]);
}
根据OP的注释进行编辑,以添加到自定义类别列表中。
internal class InputMessage
{
public string RecordID { get; set;}
public string Data { get; set;}
}
string str = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030";
string [] arr = str.Split(';');
List<InputMessage> inputMessages = new List<InputMessage>();
for(int i=0; i < arr.Length; i++)
{
string []arrItem = arr[i].Split('=');
inputMessages.Add(new InputMessage{ RecordID = arrItem[0], Data = arrItem[1]});
}
关于c# - 将半冒号分隔的字符串解析为通用列表<T>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14125069/