Reputation: 399
I have a string that looks like this:
var result = "y-9m-10y-9m-11y-0m-02y-0m-03";
I need to make 2 lists:
one for all the y-
objects(9,9,0,0)
and another for the m-
objects(10,11,02,03).
How can I do this?
I have this older code from before that doesn't care about the y-
objects. Now I need to get both sets.
var result = "m-10m-11m-02m-03";
var months = result.Split(new[] { "m-" }, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 1
Views: 102
Reputation: 9616
Quick and dirty solution using regular expressions and LINQ:
var months = Regex.Matches(result, @"m-(\d+)").Cast<Match>().Select(m => int.Parse(m.Groups[1].Value));
var years = Regex.Matches(result, @"y-(\d+)").Cast<Match>().Select(m => int.Parse(m.Groups[1].Value));
Note that this doesn't do any error checking.
Edit: In the question you seem to use the extracted strings without converting them to int
. In this case, omit the int.Parse
and use m.Groups[1].Value
directly.
Upvotes: 3