I am trying to get the input from the user in a single Line with with [, ,] separators. Like this:
[Q,W,1] [R,T,3] [Y,U,9]
And then I will use these inputs in a function like this:
f.MyFunction('Q','W',1); // Third parameter will be taken as integer
f.MyFunction('R','T',3);
f.MyFunction('Y','U',9);
I thought I could do sth like:
string input = Console.ReadLine();
string input1 = input.Split(' ')[0];
char input2 = input.Trim(',') [0];
But it seems to repeat a lot. What would be the most logical way to do this?
Sometimes a regular expression really is the best tool for the job. Use a pattern that matches the input pattern and use Regex.Matches
to extract all the possible inputs:
var funcArgRE = new Regex(@"\[(.),(.),(\d+)\]", RegexOptions.Compiled);
foreach (Match match in funcArgRE.Matches(input)) {
var g = match.Groups;
f.MyFunction(g[1].Value[0], g[2].Value[0], Int32.Parse(g[3].Value));
}