当前位置:编程学习 > C#/ASP.NET >>

字符串分段填充

1、方易做图能描述  
  有一个字符串:A-B-C-D-E-F-G-H-I-J,子字符串为:A-D、G-H,
  实现功能:找到字符串 D-G、H-J。
2、代码说明及描述
  (1)思想
    循环字符串,逐个与子字符串的第一个字符进行比对,
    如果子字符串列表中不存在,则添加子字符串,并且把子字符串的第一个字符设为该字符串;
    如果子字符串中存在该字符,则取得子字符串的最后一个字符,并且循环跳过该段。
  (2)C#代码
 1        private List<List<string>> FillMidList(List<string> stringList, List<List<string>> midList)
 2         {
 3             List<List<string>> rtn = new List<List<string>>();
 4             List<string> strList = null;//Char List
 5             string start = string.Empty;//Start Char
 6             string end = string.Empty;//End Char
 7             bool isFind = false;
 8             bool isFindEnd = false;//Find End Char
 9
10             for (int i = 0; i < stringList.Count; i++)
11             {
12                 start = stringList[i];
13                 //If exit char
14                 foreach (var item in midList)
15                 {
16                     if (start == item[0])
17                     {
18                         if (isFindEnd == true)
19                         {
20                             end = start;
21                         }
22                         isFind = true;
23                         start = item[item.Count - 1];
24                         break;
25                     }
26                 }
27
28                 if (isFind == true)
29                 {
30                     isFind = false;
31                     //Adjust index
32                     for (int j = 0; j < stringList.Count; j++)
33                     {
34                         if (stringList[j] == start)
35                         {
36                             i = j - 1;
37                             break;
38                         }
39                     }
40                     if (isFindEnd == true)
41                     {
42                         strList.Add(end);
43                         rtn.Add(strList);
44                         isFindEnd = false;
45                         end = string.Empty;
46                     }
47                 }
48                 else
49                 {
50                     if (isFindEnd == true)
51                     {
52                         //Add the last
53                         if (start == stringList[stringList.Count - 1])
54                      &

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,