Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to validate operators in my coding. After typing input I need to replace input with space which is working fine but here is one problem, if I am trying to replace >= with space it is breaking in two par > = which is wrong. I can't remove "logicdata.Replace("<", " < ").Replace(">", " > ")" from coding as I need this also.
So please suggest How can I avoid it.

Below is the coding.

var validatelist = logicdata.Replace("<", " < ").Replace(">", " > ").Replace(">=", " >= ").Replace("{", " { ").Replace("}", " } ").Replace(")", " ) ").Replace("(", " ( ").Replace("+", " + ").Replace("-", " - ").Replace("&&", " && ").Replace("==", " == ").Replace("||", " || ").Replace("!=", " != ").Replace("<=", " <= ").Replace("&=", " &= ").Replace("<<=", " <<= ").Replace(">>=", " >>= ");

Thank in advance.
Posted

1 solution

First, I have to say that I have trouble imagining what kind of scenario you'd need to this in.

But, if I had to do this:
C#
.Replace(">=", "GTE")
.Replace("<=", "LTE")
.Replace("<<=", "SFL")
.Replace(">>=", "SFR")

.Replace("<", " < ")
.Replace(">", " > ")

.Replace("GTE", " >= ")
.Replace("LTE", " <= ")
.Replace("SFL", " <<= " )
.Replace("SFR", " >>= ");
Perhaps one of our resident RegEx gurus will come along and make it easy :)
 
Share this answer
 
Comments
Philippe Mori 25-Jan-16 22:52pm    
Well, it might be a good idea to replace operators with a string that contains otherwise unused symbols like §GTE§ to avoid potential conflicts with existing text.

If this is to be done only once to normalize a code base, then that simpler solution might be more than adequate. If this is intended to be used regularly, then it would be better to use Regex... or even better since Roslyn is now open source, use it to properly parse and modify source.
BillWoodruff 25-Jan-16 23:06pm    
Hi Philipe, as I tried to imply, I have the feeling this type of processing should be avoided, if at at all possible, by application design.

Re: Roslyn and parsing; I'd like to see a good article on that; hope you will write it !

cheers, Bill
Philippe Mori 25-Jan-16 23:21pm    
I don't know much about it except that I have seen a video that show how to modify the compiler to recognize « and » as additional quotes symbol and I take a fast look on code just to have a idea of what the code look like.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900