如何使用多个分隔符作为键将字符串拆分为字典

时间:2017-05-18 09:42:53

标签: c# string

我的字符串看起来像这样:

var givenString = "Id: some id Title: sometitle Descritpion: some description Criteria: some criteria <br>more criteria"

如何将其拆分为字典,其中separator是键,值是来自给定的字符串。此外,有可能其中一个分隔符不在文本中。

我知道如何将其拆分为句子但不知道当其中一个分隔符丢失时如何处理情况以及如何将其拆分为字典。

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"

EDIT2 我忘记了一些分隔符可以是两个单词:(。如果它更容易我可以要求更改分隔符用大写字母写的:

var givenString = "ID: some id TITLE: sometitle CRITERIA: some criteria <br>more criteria, DIFFERENT CRITERIA: some criteria <br>more criteria"

1 个答案:

答案 0 :(得分:4)

为了拆分模式(字母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"