我已经登陆了一列看起来像这样的数据:
1[::]One[::]2[::]Two[::]3[::]Three
Run Code Online (Sandbox Code Playgroud)
如何将其拆分为C#中的Dictionary?给:
1,"One"
2,"Two"
3,"Three"
Run Code Online (Sandbox Code Playgroud)
正则表达式是正确的方法吗?
我已经走到了这一步,\d+\[::]但我不确定下一步该做什么
你真的不需要正则表达式; 只是使用String.Split(String[], ...)方法拆分分隔符:
var str = "1[::]One[::]2[::]Two[::]3[::]Three";
var parts = str.Split(new [] { "[::]" }, int.MaxValue, StringSplitOptions.None);
// parts is an array that contains: "1", "One", "2", "Two", "3", "Three"
var dict = new Dictionary<int, string>();
for (int i = 0; i < parts.Length; i += 2)
dict.Add(int.Parse(parts[i]), parts[i + 1]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |