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!
If a match is at one end of the string, you will get an empty match next to it. Likewise, if there are two adjacent matches, the string will be split on both of them, so you end up with an empty string in between. Citing MSDN:
and
Just filter them out in a second step.
Also, please make your life easier and use verbatim strings:
The second expression could be simplified even further:
Why not just remove the white spaces and then split the string with normal string handling functions? Like this...
Why bother making this more complicated than it needs to be?
m.buettner's answer is correct. Also consider that you can do this in one step. From MSDN:
Therefore, if you include the whitespace in the split pattern but outside the capturing parentheses, you can split on it as well but not include it in the result array:
The result:
And your final result would be:
I found the solution I was looking for thanks to all of your replies.
By doing this it seperates the characters of the String into parts based on the regular mathematical operators (+, -, *, /) and parenthesis. After doing this it eliminates any remaining empty Strings within the
substrings
A non-regex solution would probably be String.Replace - you could simply replace " ", "\t", and other whitespace with the empty string "".