I have a string e.g "number1<=number2&&number3>number4||number2=number4" and having operator list as -
var operators = new List<string> {"=", "!", "<", ">", ">=", "<=", "!=","||","&&"};
So, expectation is to introduce a single space before and after each operator in the string.
"number1 <= number2 && number3 > number4 || number2 = number4"
I tried following code but its not working for instance, e.g, <, >=
public static string AddSpaceBeforeAndAfterOperator(string expression) {
var operators = new List<string> {"=", "!", "<", ">", ">=", "<=", "!=", "||", "&&"};
foreach (var op in operators) {
var index = expression.IndexOf(op, StringComparison.Ordinal);
if (index >= 0) {
if (expression.Substring(index - 1) != " ") {
expression = expression.Insert(index-1, " ");
}
expression = expression.Insert(index + op.Length + 1, " ");
}
}
return expression;
}
Note: The Operator list is coming as random.
Any help would be appreciated!