I have a string variable that contain [l=9;f=0;r=5;p=2]. There are may be a more than one:[l=9;f=0;r=5;p=2],[l=9;f=0;r=6;p=2].
I want to get List of Tuple<int,int,int>, where item1 = value of l, item2= value of r, item3=value of p. What is best approach to do this?
My code:
// split string
var splittedFormatedSeats = Regex.Matches(formatSeats, @"\[.+?\]")
.Cast<Match>()
.Select(m => m.Value)
.ToList();
IList<Tuple<int,int,int>> fullSeat = new List<Tuple<int, int, int>>();
foreach (var splittedFormatedSeat in splittedFormatedSeats)
{
...
}
Thanks.
I would adopt another strategy. First, split subcomponents of your string
This will give you an array of string. This will be far more efficient to parse small chunks of strings instead of a big string.
You can then use either a
Regexto extract the values or a simple code like this :This will produces an enumeration of an anonymous type.
If you actually need a
Tuplesimply change the code to :I would however, advocate for writing a small struct to simplify the code and raise its readability.
Finally, if the string always have this form, you could avoid using the Regex.