.net - Removing White Space: C# -
i trying remove white space
exists in string input
. ultimate goal create infix evaluator, having issues parsing input expression.
it seems me easy solution using regular expression
function, namely regex.replace(...)
here's have far..
infixexp = regex.replace(infixexp, "\\s+", string.empty); string[] substrings = regex.split(infixexp, "(\\()|(\\))|(-)|(\\+)|(\\*)|(/)");
assuming user inputs infix expression (2 + 3) * 4, expect break string array {(, 2, +, 3, ), *, 4}
; however, after debugging, getting following output:
infixexp = "(2+3)*7" substrings = {"", (, 2, +, 3, ), "", *, 7}
it appears white space being removed infix expression, splitting resulting string improper.
could give me insight why? likewise, if have constructive criticism or suggestions, let me know!
if match @ 1 end of string, empty match next it. likewise, if there 2 adjacent matches, string split on both of them, end empty string in between. citing msdn:
if multiple matches adjacent 1 another, empty string inserted array. example, splitting string on single hyphen causes returned array include empty string in position 2 adjacent hyphens found [...].
and
if match found @ beginning or end of input string, empty string included @ beginning or end of returned array.
just filter them out in second step.
also, please make life easier , use verbatim strings:
infixexp = regex.replace(infixexp, @"\s+", string.empty); string[] substrings = regex.split(infixexp, @"(\(|\)|-|\+|\*|/)");
the second expression simplified further:
@"([()+*/-])"
Comments
Post a Comment