Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit d211483

Browse files
committed
Add ParseAsKeyValues to return KVP's
1 parent 778ea9e commit d211483

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,21 @@ public static Dictionary<string,string> ParseKeyValueText(this string text, stri
11631163
return to;
11641164
}
11651165

1166+
public static List<KeyValuePair<string,string>> ParseAsKeyValues(this string text, string delimiter=" ")
1167+
{
1168+
var to = new List<KeyValuePair<string,string>>();
1169+
if (text == null) return to;
1170+
1171+
foreach (var parts in text.ReadLines().Select(line => line.Trim().SplitOnFirst(delimiter)))
1172+
{
1173+
var key = parts[0].Trim();
1174+
if (key.Length == 0 || key.StartsWith("#")) continue;
1175+
to.Add(new KeyValuePair<string, string>(key, parts.Length == 2 ? parts[1].Trim() : null));
1176+
}
1177+
1178+
return to;
1179+
}
1180+
11661181
public static IEnumerable<string> ReadLines(this string text)
11671182
{
11681183
string line;

0 commit comments

Comments
 (0)