Get correct string position C# -
i have string:
string str = "wishing {all great} day {ahead}. \n lot \n } \n {your} help!}"
i read string line line. @ present have 4 lines me:
1. wishing {all great} day {ahead}. 2. lot 3. } 4. {your} help!}
the intention check whether "}" closing brace of string , should not appear single character in of other lines.
my approach this:
in above string, want position of "}" in main string. , check whether there characters after position check whether closing brace.
but iam unable correct position of "}" may appear in other lines well.
is there better way go this?
within single string, sounds should iterate on characters in string , keep count of how many opening , how many closing braces you've seen. this:
int open = 0; (int = 0; < text.length; i++) { switch (text[i]) { case '{': open++; break; case '}': if (open > 0) { open--; // matches opening brace } else { // whatever want unmatched brace } break; default: // nothing break; } }
if need know where opening brace was, might want stack<int>
instead of count. when see {
push index (i
) onto stack. when see }
, if stack isn't empty pop find matching opening brace.
next need consider whether ever need escape braces remove "special" meaning... @ point things become more complicated again.
Comments
Post a Comment