用列表框中的一些文本分隔字符串
separating strings by some text within listbox
我有一个ListBox
,其中table个名字是这样写的:
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
我会为此使用 Dictionary<string, List<string>>
。解析的每个 'section' 将是一个键,其余部分将是值。
Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>();
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);
if(!myDict.ContainsKey(sectionNum))
{
myDict.Add(sectionNum, new List<string> { someOtherValue });
}
else
{
myDict[sectionNum].Add(someOtherValue);
}
}
}
除非我完全误解了你的问题,否则我认为这是对你的动态对象的潜在解决方案。
你可以这样做:
var sections = new Dictionary<string, List<string>>();
foreach(var it in uploadBox.Items)
{
var item = it.ToString();
if(item.Contains("Section"))
{
var section = GetSection(item);
if(!sections.ContainsKey(section))
{
sections.Add(section, new List<string>());
}
sections[section].Add(item);
}
}
private string GetSection(string item)
{
var split = item.Split("_");
return $"{split[1]}_{split[2]}";
}
这种任务最好用正则表达式:
uploadBox.Items
.GroupBy(x => Regex.Match(x.ToString(), @"^\w+_Section_(?<section>\d+)").Groups["section"].Value)
我有一个ListBox
,其中table个名字是这样写的:
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
我会为此使用 Dictionary<string, List<string>>
。解析的每个 'section' 将是一个键,其余部分将是值。
Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>();
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);
if(!myDict.ContainsKey(sectionNum))
{
myDict.Add(sectionNum, new List<string> { someOtherValue });
}
else
{
myDict[sectionNum].Add(someOtherValue);
}
}
}
除非我完全误解了你的问题,否则我认为这是对你的动态对象的潜在解决方案。
你可以这样做:
var sections = new Dictionary<string, List<string>>();
foreach(var it in uploadBox.Items)
{
var item = it.ToString();
if(item.Contains("Section"))
{
var section = GetSection(item);
if(!sections.ContainsKey(section))
{
sections.Add(section, new List<string>());
}
sections[section].Add(item);
}
}
private string GetSection(string item)
{
var split = item.Split("_");
return $"{split[1]}_{split[2]}";
}
这种任务最好用正则表达式:
uploadBox.Items
.GroupBy(x => Regex.Match(x.ToString(), @"^\w+_Section_(?<section>\d+)").Groups["section"].Value)