次の文字列があります
MyKey1=MyVal1
MyKey2=MyVal2
MyKey3=MyVal3
MyKey3=MyVal3
まず、行に分割する必要があるため、各行を ' =
' 文字で分割して、その行からキーと値を取得する必要があります。結果として、私が欲しいのはList<KeyValuePair<string, string>>
(なぜではないのDictionary
ですか?=>リスト内に重複するキーがあるかもしれません)ので、.ToDictionary()
拡張機能を使用できません。
私は次のことにかなりこだわっています:
List<KeyValuePair<string, string>> fields =
(from lines in Regex.Split(input, @"\r?\n|\r", RegexOptions.None)
where !String.IsNullOrWhiteSpace(lines)
.Select(x => x.Split(new [] { '='}, 2, StringSplitOptions.RemoveEmptyEntries))
.ToList()
--> select new KeyValuePair? Or with 'let' for splitting by '='?
what about exception handling (e.g. ignoring empty values)