I have a text file from which I want to store Keys
and Values
in a String
array.
In this case, Key
is something like &
Here's one way to do it with regex (comments in code):
string[] lines = {@"Input File : 'D:\myfile.wav'", @"Duration: 00:00:18.57"};
Regex regex = new Regex("^[^:]+");
Dictionary dict = new Dictionary();
for (int i = 0; i < lines.Length; i++)
{
// match in the string will be everything before first :,
// then we replace match with empty string and remove first
// character which will be :, and that will be the value
string key = regex.Match(lines[i]).Value.Trim();
string value = regex.Replace(lines[i], "").Remove(0, 1).Trim();
dict.Add(key, value);
}
It uses pattern ^[^:]+
, which is negated class technique to match everything unless specified character.