Split string expression with multiple parenthesis (C#) -
i have below string , want split in such way both parameters of function fngetdate
separated.
function:
"fngetdate('d',-1+ cint(cbool(datepart('w',date())<=2)) + cint(cbool(datepart('w',date())=2)))"
desired output (after split):
[0] fngetdate( [1] 'd' [2] -1+ cint(cbool(datepart('w',date())<=2)) + cint(cbool(datepart('w',date())=2))
according comment, counting number of opening , closing parenthesis ok you.
this example works me:
string str = "fngetdate('d',-1+ cint(cbool(datepart('w',date())<=2)) + cint(cbool(datepart('w',date())=2)))"; int parlevel = 0; list<string> arguments = new list<string>(); string currentstring = string.empty; foreach (char t in str) { switch (t) { case '(': if (t == '(') parlevel++; currentstring += t; if (parlevel == 1) { arguments.add(currentstring); currentstring = string.empty; } break; case ')': if (t == ')') parlevel--; if (parlevel > 0) currentstring += t; break; case ',': if (parlevel == 1) { arguments.add(currentstring); currentstring = string.empty; } else currentstring += t; break; default: currentstring += t; break; } } if (!string.isnullorempty(currentstring)) arguments.add(currentstring); (int = 0; < arguments.count; i++) console.writeline("argument {0}: {1}", i, arguments[i]);
Comments
Post a Comment