首页 > 编程语言 >C# 字符串处理

C# 字符串处理

时间:2023-05-16 14:56:34浏览次数:58  
标签:处理 C# subKeyList results int dict subKey key 字符串

原字符串:“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

标签:处理,C#,subKeyList,results,int,dict,subKey,key,字符串
From: https://www.cnblogs.com/cloud-sword/p/17405628.html

相关文章

  • 多个 ComboBox 控件绑定同一数据源,数据会联动(其中一个选择项改变的时候,其余也会跟着变
    问题:在Winform开发中,两个ComboBox控件绑定了同一个数据源List<T>,但是在使用的时候发现,选择其中一个ComboBox的时候,另一个也会跟着变化。原因:内存中只有一份数据,改变任何一个ComboBox都会使得数据源有所变化,导致其他ComboBox的展示效果发生联动。解决:将数据源进行复制,相当于为每......
  • 解决Microsoft Edge 浏览器 出现“无法访问该页面”问题
    问题分析:浏览器“出现了“无法访问该页面”问题”——网络明明连接正常,网页就是上不了网无法打开。解决方法: 打开【控制面板】>【网络和Internet】>【Internet选项】选择【连接】选项卡,出现如下界面。 点击下方的的“局域网设置(L)”,弹出如下界面。 点击确定--确......
  • [Qt]connect()参数Qt:ConnectionType使用讲解
    https://blog.csdn.net/humanking7/article/details/86064859 创文章,欢迎转载。转载请注明:转载自祥的博客原文链接:https://blog.csdn.net/humanking7/article/details/86064859文章目录@[toc]1.问题来源2.参数详解3.使用建议connect()参数Qt:ConnectionType使用讲解1.问题来......
  • 用QWebsocket时关于信号槽的一个坑
    https://blog.csdn.net/zerolity/article/details/94746977 坑描述:connect(&m_webSocket,&QWebSocket::textMessageReceived,this,&BWebsocket::onTextMessageReceived);1和主机通过websocket通信。接收主机发的指令有时导致重复接收。信号发送者和接受者同一线程。onTe......
  • Kafka 集群安装 docker-compose安装
    目录Kafka集群安装docker-compose安装docker-compose.yml安装Kafka集群安装docker-compose安装docker-compose.ymlversion:"2"services:zookeeper:image:docker.io/bitnami/zookeepercontainer_name:zookeeperports:-"2181:2181"......
  • Docker 常用
    删除无用镜像低版本dockerrmi$(dockerimages-qa)高版本dockerimageprune-a自定义(借助awk和xargs)dockerimages|grepmytag|awk-F""'{print$3}'|xargsdockerrmi......
  • Best practices for customization | Kentico 12 Service Pack Documentation (xperie
    Bestpracticesforcustomization|Kentico12ServicePackDocumentation(xperience.io)UsingjQuery Note:TheinformationinthissectiononlyappliestotheKenticoadministrationinterfaceandsitesbuiltusingthePortalEnginedevelopmentmodel,......
  • 《啊哈C语言——逻辑的挑战》学习笔记
    第一章梦想启航第1节让计算机开口说话1、基础知识1)计算机“说话”的两种方式显示在屏幕上通过喇叭发出声音2)计算机“说话”之显示在屏幕上格式:printf("");注意:printf要加“f”printf后要加括号()双引号""内是要计算机“说的内容”所有符号全在英文符号环境下输入分......
  • @Getter cheated me
    下面这段代码,IDE里正常显示。不过,在build时,会报错。interfaceDoable{IntegergetCode();}@lombok.GetterclassDerivedClassimplementsDoable{intcode;}错误信息:Error:(11,5)java:DerivedClass不是抽象的,并且未覆盖Doable中的抽象方法getCode()Err......
  • Newtonsoft.JSON 自定义JsonConverter
    引用:https://www.cjavapy.com/article/2513/https://www.cnblogs.com/weihanli/p/11080531.htmlhttps://www.cnblogs.com/Lulus/p/16968656.htmlhttps://www.cjavapy.com/article/2513/https://www.cnblogs.com/net-sky/p/16563025.htmlpublicclassDecimalConver......