Wednesday, 28 August 2013

Removing White Space: C#

Removing White Space: C#

I am trying to remove white space that exists in a String input. My
ultimate goal is to create an infix evaluator, but I am having issues with
parsing the input expression.
It seems to me that the easy solution to this is using a Regular
Expression function, namely Regex.Replace(...)
Here's what I have so far..
infixExp = Regex.Replace(infixExp, "\\s+", string.Empty);
string[] substrings = Regex.Split(infixExp,
"(\\()|(\\))|(-)|(\\+)|(\\*)|(/)");
Assuming the user inputs the infix expression (2 + 3) * 4, I would expect
that this would break the string into the array {(, 2, +, 3, ), *, 4};
however, after debugging, I am getting the following output:
infixExp = "(2+3)*7"
substrings = {"", (, 2, +, 3, ), "", *, 7}
It appears that the white space is being properly removed from the infix
expression, but splitting the resulting string is improper.
Could anyone give me insight as to why? Likewise, if you have any
constructive criticism or suggestions, let me know!

No comments:

Post a Comment