C# 字符串处理
原字符串:“0,13,1:0,12,0:1,2,1:1,1,0”
要求输出字符串(降序):
1 string input = "0,13,1:0,12,0:1,2,1:1,1,0";
2 SortedDictionary<int, SortedDictionary<int, int>> dict = new SortedDictionary<int, SortedDictionary<int, int>>();
3
4 string[] parts = input.Split(':');
5 foreach (string part in parts)
6 {
7 int[] values = Array.ConvertAll(part.Split(','), int.Parse);
8
9 int key = values[0];
10 int subKey = values[1];
11 int value = values[2];
12
13 if (!dict.ContainsKey(key))
14 dict[key] = new SortedDictionary<int, int>();
15
16 dict[key][subKey] = value;
17 }
18
19 List<string> results = new List<string>();
20 foreach (int key in dict.Keys)
21 {
22 SortedDictionary<int, int> subDict = dict[key];
23
24 int? lastSubKey = null;
25 List<int> subKeyList = new List<int>();
26 List<string> valueList = new List<string>();
27
28 foreach (int subKey in subDict.Keys.OrderByDescending(x => x))
29 {
30 if (lastSubKey.HasValue && subKey != lastSubKey - 1)
31 {
32 if (subKeyList.Count == 1)
33 results.Add($"{key},{subKeyList[0]},{subDict[subKeyList[0]]}");
34 else
35 results.Add($"{key},{subKeyList[0]}-{subKeyList[subKeyList.Count - 1]},{string.Join("", valueList)}");
36
37 subKeyList.Clear();
38 valueList.Clear();
39 }
40
41 subKeyList.Add(subKey);
42 valueList.Add(subDict[subKey].ToString());
43 lastSubKey = subKey;
44 }
45
46 if (subKeyList.Count == 1)
47 results.Add($"{key},{subKeyList[0]},{subDict[subKeyList[0]]}");
48 else if (subKeyList.Count > 1)
49 results.Add($"{key},{subKeyList[0]}-{subKeyList[subKeyList.Count - 1]},{string.Join("", valueList)}");
50 }
51
52 foreach (string result in results)
53 Console.WriteLine(result);
输出结果:
0,13-12,10
1,2-1,10
要求输出字符串(升序):
1 string input = "0,13,1:0,12,0:1,2,1:1,1,0";
2 SortedDictionary<int, SortedDictionary<int, int>> dict = new SortedDictionary<int, SortedDictionary<int, int>>();
3
4 string[] parts = input.Split(':');
5 foreach (string part in parts)
6 {
7 int[] values = Array.ConvertAll(part.Split(','), int.Parse);
8
9 int key = values[0];
10 int subKey = values[1];
11 int value = values[2];
12
13 if (!dict.ContainsKey(key))
14 dict[key] = new SortedDictionary<int, int>();
15
16 dict[key][subKey] = value;
17 }
18
19 List<string> results = new List<string>();
20 foreach (int key in dict.Keys)
21 {
22 SortedDictionary<int, int> subDict = dict[key];
23
24 int? lastSubKey = null;
25 List<int> subKeyList = new List<int>();
26 List<string> valueList = new List<string>();
27
28 foreach (int subKey in subDict.Keys)
29 {
30 if (lastSubKey.HasValue && subKey != lastSubKey + 1)
31 {
32 if (subKeyList.Count == 1)
33 results.Add($"{key},{subKeyList[0]},{subDict[subKeyList[0]]}");
34 else
35 results.Add($"{key},{subKeyList[0]}-{subKeyList[subKeyList.Count - 1]},{string.Join("", valueList)}");
36
37 subKeyList.Clear();
38 valueList.Clear();
39 }
40
41 subKeyList.Add(subKey);
42 valueList.Add(subDict[subKey].ToString());
43 lastSubKey = subKey;
44 }
45
46 if (subKeyList.Count == 1)
47 results.Add($"{key},{subKeyList[0]},{subDict[subKeyList[0]]}");
48 else if (subKeyList.Count > 1)
49 results.Add($"{key},{subKeyList[0]}-{subKeyList[subKeyList.Count - 1]},{string.Join("", valueList)}");
50 }
51
52 foreach (string result in results)
53 Console.WriteLine(result);
输出结果:
0,12-13,01
1,1-2,01

浙公网安备 33010602011771号