I'm doing a program that reads any math expression such as x^(3*x+1) + (cos x)
, does not necessarily have to be that expression, and I'd like to know how to get the specific snippet between strings in the string.
I know that with IndexOf
and substring
I can get what is inside the first parentheses, but when I have more than 1 I do not know what to do.
EDIT: What I want to do is to get the string that is between the parentheses in the 3*x + 1" e "cos x
case, but I do not know how to get the contents of the second parenthesis, cos x
, since I only know how to do this using indexof
and substring this way:
string formula = Console.ReadLine();
string trecho;
int pos1, pos2;
pos1 = formula.IndexOf('(');
pos2 = formula.IndexOf(')');
trecho = formula.Substring(pos1 + 1 , pos2 - 1);
Console.WriteLine($"{trecho}");
I would like to know if there is any way to get the snippet that is in the second parenthesis as well.