【发布时间】:2019-04-10 19:46:41
【问题描述】:
我有一个ListBox,其中表名是这样写的:
Staging_Section_01_2019_03_19_01
Staging_Section_01_2019_03_20_01
Staging_Section_23_2019_03_21_01
Staging_Section_52_2019_03_23_01
Staging_Section_52_2019_03_24_01
我要做的是按节号将它们分开,所以我希望所有Section_01 都在一个List 对象中,Section_23 在另一个List 对象中,依此类推。动态的性质让我很难。
到目前为止,我有以下内容:
foreach (var it in uploadBox.Items)
{
if (it.ToString().Contains("Section"))
{
section = it.ToString().Substring(0, 18);
found = it.ToString().IndexOf("_");
section = section.Substring(found + 1);
sectionNum = section.Substring(8, 2);
}
}
我得到了sectionNum,它只是数字和部分,就像Section_01 这样的字符串。
知道如何解决这个问题吗?
预期的输出是这样的:
列表 1
Staging_Section_01_2019_03_19_01
Staging_Section_01_2019_03_20_01
清单 2
Staging_Section_23_2019_03_21_01
清单 3
Staging_Section_52_2019_03_23_01
Staging_Section_52_2019_03_24_01
【问题讨论】:
-
这看起来非常适合正则表达式。
-
我认为操作的问题是如何将它们按编号分开。
-
请向我们展示您的预期输出示例
-
还有,是否有可能有一个 Section_100 或一个 Section_1000?
-
总是Staging_Section_##?如果是这样,只需存储当前子字符串,如果该子字符串与您存储的子字符串不同,则在下一个循环中创建一个新列表并添加当前列表。每次节省时间,以确保您比较之前的变化。