我有如下所示的字符串:
var givenString = "Id: some id Title: sometitle Descritpion: some description Criteria: some criteria <br>more criteria"
如何将其拆分为字典,其中分隔符是键,值来自给定字符串。也有可能其中一个分隔符不在文本中。
我知道如何将其拆分为句子,但不知道如何处理缺少其中一个分隔符的情况以及如何将其拆分为字典。
string[] separators = { "Id:", "Title:", "Descritpion", "Criteria:" };
string[] words = givenString.Split(separators, StringSplitOptions.None);
EDIT1:
缺少分隔符的示例:
var givenString = "Id: some id Title: sometitle Criteria: some criteria <br>more criteria"
编辑2
我忘记了一些分隔符可以是两个单词 :(。如果这样更容易,我可以要求更改分隔符为大写字母:
var givenString = "ID: some id TITLE: sometitle CRITERIA: some criteria <br>more criteria, DIFFERENT CRITERIA: some criteria <br>more criteria"
最佳答案
为了拆分模式(字母 A..Za..z
后跟列 :
),我建议使用正则表达式, Regex.Split 而不是 givenString.Split
:
string givenString =
@"Id: some id Title: sometitle Descritpion: some description Criteria: some criteria <br>more criteria";
Dictionary<string, string> result = Regex
.Split(givenString, "([A-Z][a-z]+ [A-Z][a-z]+:)|([A-Z][a-z]+:)")
.Skip(1) // <- skip (empty) chunk before the 1st separator
.Select((item, index) => new { // pair: separator followed by value
value = item.Trim(),
index = index / 2 })
.GroupBy(item => item.index)
.ToDictionary(chunk => chunk.First().value.TrimEnd(':'),
chunk => chunk.Last().value);
测试:
string report = string.Join(Environment.NewLine, result
.Select(pair => $"Key = {pair.Key,-12} Value = \"{pair.Value}\""));
Console.Write(report);
结果:
Key = Id Value = "some id"
Key = Title Value = "sometitle"
Key = Descritpion Value = "some description"
Key = Criteria Value = "some criteria <br>more criteria"
关于c# - 如何使用多个分隔符作为键将字符串拆分为字典,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44044202/