How to add space before and after contional operator in string?












2















I have the string e.g "number1<=number2&&number3>number4||number2=number4" and having operator list as -



var operators = new List<string> {"=", "!", "<", ">", ">=", "<=", "!=","||","&&"};


So expectation is need to introduce single space before and after the operator in string.



"number1 <= number2 && number3 > number4 || number2 = number4"



I have tried following option but its not working in case of eg. <, >=



    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: Operator list is coming as random.



Any help appreciated!










share|improve this question























  • You could construct a regex by "(" + string.Join("|", operators.Select(Regex.Escape)) + ")" and then doing a regex.Replace

    – Klaus Gütter
    Nov 15 '18 at 6:45
















2















I have the string e.g "number1<=number2&&number3>number4||number2=number4" and having operator list as -



var operators = new List<string> {"=", "!", "<", ">", ">=", "<=", "!=","||","&&"};


So expectation is need to introduce single space before and after the operator in string.



"number1 <= number2 && number3 > number4 || number2 = number4"



I have tried following option but its not working in case of eg. <, >=



    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: Operator list is coming as random.



Any help appreciated!










share|improve this question























  • You could construct a regex by "(" + string.Join("|", operators.Select(Regex.Escape)) + ")" and then doing a regex.Replace

    – Klaus Gütter
    Nov 15 '18 at 6:45














2












2








2








I have the string e.g "number1<=number2&&number3>number4||number2=number4" and having operator list as -



var operators = new List<string> {"=", "!", "<", ">", ">=", "<=", "!=","||","&&"};


So expectation is need to introduce single space before and after the operator in string.



"number1 <= number2 && number3 > number4 || number2 = number4"



I have tried following option but its not working in case of eg. <, >=



    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: Operator list is coming as random.



Any help appreciated!










share|improve this question














I have the string e.g "number1<=number2&&number3>number4||number2=number4" and having operator list as -



var operators = new List<string> {"=", "!", "<", ">", ">=", "<=", "!=","||","&&"};


So expectation is need to introduce single space before and after the operator in string.



"number1 <= number2 && number3 > number4 || number2 = number4"



I have tried following option but its not working in case of eg. <, >=



    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: Operator list is coming as random.



Any help appreciated!







c# string c#-4.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 6:39









Sumit DeshpandeSumit Deshpande

1,62611326




1,62611326













  • You could construct a regex by "(" + string.Join("|", operators.Select(Regex.Escape)) + ")" and then doing a regex.Replace

    – Klaus Gütter
    Nov 15 '18 at 6:45



















  • You could construct a regex by "(" + string.Join("|", operators.Select(Regex.Escape)) + ")" and then doing a regex.Replace

    – Klaus Gütter
    Nov 15 '18 at 6:45

















You could construct a regex by "(" + string.Join("|", operators.Select(Regex.Escape)) + ")" and then doing a regex.Replace

– Klaus Gütter
Nov 15 '18 at 6:45





You could construct a regex by "(" + string.Join("|", operators.Select(Regex.Escape)) + ")" and then doing a regex.Replace

– Klaus Gütter
Nov 15 '18 at 6:45












2 Answers
2






active

oldest

votes


















3














I would recommend a regex solution.



First, you need to escape all your operators, join them together with |:



var operatorsString = string.Join("|", 
operators.OrderByDescending(x => x.Length).Select(Regex.Escape).ToArray()
);
// OrderByDescending here because we want the longer operators to be matched first.


Next, create the regex:



var regex = $"\s*({operatorString})\s*";


Using the operators array in the question, the array looks like this:



s*(<=|>=|!=|==||||&&|=|!|<|>)s?*


Note that s* is used to check if the operator is already surrounded by spaces. If it is, those spaces will be matched and replaced.



The replacement is:



 $1 


Code:



Regex.Replace(input, regex, " $1 ");


Note the leading and trailing space.



Also note that Regex is inside the System.Text.RegularExpressions namespace.



Demo






share|improve this answer


























  • Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

    – Dmitry Bychenko
    Nov 15 '18 at 7:55













  • @DmitryBychenko Good point. Edited.

    – Sweeper
    Nov 15 '18 at 8:21











  • @Sweeper - Thanks for the suggestion.

    – Sumit Deshpande
    Nov 16 '18 at 5:14



















1














Im not sure if this is the most effective way to do this, but is is rally simple by using replace



public static string AddSpaceBeforeAndAfterOperator(string expression)
{
var operators = new List<string> { "=", "!", "<", ">", ">=", "<=", "!=", "||", "&&" };
foreach (var op in operators)
{
expression = expression.Replace(op, " " + op + " ");
}

return expression;
}





share|improve this answer



















  • 2





    Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

    – Sumit Deshpande
    Nov 15 '18 at 6:52













  • wont work for <= will result < =

    – styx
    Nov 15 '18 at 6:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313758%2fhow-to-add-space-before-and-after-contional-operator-in-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I would recommend a regex solution.



First, you need to escape all your operators, join them together with |:



var operatorsString = string.Join("|", 
operators.OrderByDescending(x => x.Length).Select(Regex.Escape).ToArray()
);
// OrderByDescending here because we want the longer operators to be matched first.


Next, create the regex:



var regex = $"\s*({operatorString})\s*";


Using the operators array in the question, the array looks like this:



s*(<=|>=|!=|==||||&&|=|!|<|>)s?*


Note that s* is used to check if the operator is already surrounded by spaces. If it is, those spaces will be matched and replaced.



The replacement is:



 $1 


Code:



Regex.Replace(input, regex, " $1 ");


Note the leading and trailing space.



Also note that Regex is inside the System.Text.RegularExpressions namespace.



Demo






share|improve this answer


























  • Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

    – Dmitry Bychenko
    Nov 15 '18 at 7:55













  • @DmitryBychenko Good point. Edited.

    – Sweeper
    Nov 15 '18 at 8:21











  • @Sweeper - Thanks for the suggestion.

    – Sumit Deshpande
    Nov 16 '18 at 5:14
















3














I would recommend a regex solution.



First, you need to escape all your operators, join them together with |:



var operatorsString = string.Join("|", 
operators.OrderByDescending(x => x.Length).Select(Regex.Escape).ToArray()
);
// OrderByDescending here because we want the longer operators to be matched first.


Next, create the regex:



var regex = $"\s*({operatorString})\s*";


Using the operators array in the question, the array looks like this:



s*(<=|>=|!=|==||||&&|=|!|<|>)s?*


Note that s* is used to check if the operator is already surrounded by spaces. If it is, those spaces will be matched and replaced.



The replacement is:



 $1 


Code:



Regex.Replace(input, regex, " $1 ");


Note the leading and trailing space.



Also note that Regex is inside the System.Text.RegularExpressions namespace.



Demo






share|improve this answer


























  • Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

    – Dmitry Bychenko
    Nov 15 '18 at 7:55













  • @DmitryBychenko Good point. Edited.

    – Sweeper
    Nov 15 '18 at 8:21











  • @Sweeper - Thanks for the suggestion.

    – Sumit Deshpande
    Nov 16 '18 at 5:14














3












3








3







I would recommend a regex solution.



First, you need to escape all your operators, join them together with |:



var operatorsString = string.Join("|", 
operators.OrderByDescending(x => x.Length).Select(Regex.Escape).ToArray()
);
// OrderByDescending here because we want the longer operators to be matched first.


Next, create the regex:



var regex = $"\s*({operatorString})\s*";


Using the operators array in the question, the array looks like this:



s*(<=|>=|!=|==||||&&|=|!|<|>)s?*


Note that s* is used to check if the operator is already surrounded by spaces. If it is, those spaces will be matched and replaced.



The replacement is:



 $1 


Code:



Regex.Replace(input, regex, " $1 ");


Note the leading and trailing space.



Also note that Regex is inside the System.Text.RegularExpressions namespace.



Demo






share|improve this answer















I would recommend a regex solution.



First, you need to escape all your operators, join them together with |:



var operatorsString = string.Join("|", 
operators.OrderByDescending(x => x.Length).Select(Regex.Escape).ToArray()
);
// OrderByDescending here because we want the longer operators to be matched first.


Next, create the regex:



var regex = $"\s*({operatorString})\s*";


Using the operators array in the question, the array looks like this:



s*(<=|>=|!=|==||||&&|=|!|<|>)s?*


Note that s* is used to check if the operator is already surrounded by spaces. If it is, those spaces will be matched and replaced.



The replacement is:



 $1 


Code:



Regex.Replace(input, regex, " $1 ");


Note the leading and trailing space.



Also note that Regex is inside the System.Text.RegularExpressions namespace.



Demo







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 8:20

























answered Nov 15 '18 at 6:54









SweeperSweeper

68.8k1073140




68.8k1073140













  • Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

    – Dmitry Bychenko
    Nov 15 '18 at 7:55













  • @DmitryBychenko Good point. Edited.

    – Sweeper
    Nov 15 '18 at 8:21











  • @Sweeper - Thanks for the suggestion.

    – Sumit Deshpande
    Nov 16 '18 at 5:14



















  • Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

    – Dmitry Bychenko
    Nov 15 '18 at 7:55













  • @DmitryBychenko Good point. Edited.

    – Sweeper
    Nov 15 '18 at 8:21











  • @Sweeper - Thanks for the suggestion.

    – Sumit Deshpande
    Nov 16 '18 at 5:14

















Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

– Dmitry Bychenko
Nov 15 '18 at 7:55







Probably, a better choice s* instead of s? for "if the operator is already surrounded by spaces" (what if we have several spaces: "a___>=__b"?)

– Dmitry Bychenko
Nov 15 '18 at 7:55















@DmitryBychenko Good point. Edited.

– Sweeper
Nov 15 '18 at 8:21





@DmitryBychenko Good point. Edited.

– Sweeper
Nov 15 '18 at 8:21













@Sweeper - Thanks for the suggestion.

– Sumit Deshpande
Nov 16 '18 at 5:14





@Sweeper - Thanks for the suggestion.

– Sumit Deshpande
Nov 16 '18 at 5:14













1














Im not sure if this is the most effective way to do this, but is is rally simple by using replace



public static string AddSpaceBeforeAndAfterOperator(string expression)
{
var operators = new List<string> { "=", "!", "<", ">", ">=", "<=", "!=", "||", "&&" };
foreach (var op in operators)
{
expression = expression.Replace(op, " " + op + " ");
}

return expression;
}





share|improve this answer



















  • 2





    Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

    – Sumit Deshpande
    Nov 15 '18 at 6:52













  • wont work for <= will result < =

    – styx
    Nov 15 '18 at 6:53
















1














Im not sure if this is the most effective way to do this, but is is rally simple by using replace



public static string AddSpaceBeforeAndAfterOperator(string expression)
{
var operators = new List<string> { "=", "!", "<", ">", ">=", "<=", "!=", "||", "&&" };
foreach (var op in operators)
{
expression = expression.Replace(op, " " + op + " ");
}

return expression;
}





share|improve this answer



















  • 2





    Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

    – Sumit Deshpande
    Nov 15 '18 at 6:52













  • wont work for <= will result < =

    – styx
    Nov 15 '18 at 6:53














1












1








1







Im not sure if this is the most effective way to do this, but is is rally simple by using replace



public static string AddSpaceBeforeAndAfterOperator(string expression)
{
var operators = new List<string> { "=", "!", "<", ">", ">=", "<=", "!=", "||", "&&" };
foreach (var op in operators)
{
expression = expression.Replace(op, " " + op + " ");
}

return expression;
}





share|improve this answer













Im not sure if this is the most effective way to do this, but is is rally simple by using replace



public static string AddSpaceBeforeAndAfterOperator(string expression)
{
var operators = new List<string> { "=", "!", "<", ">", ">=", "<=", "!=", "||", "&&" };
foreach (var op in operators)
{
expression = expression.Replace(op, " " + op + " ");
}

return expression;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 6:47









Daniel W.Daniel W.

226111




226111








  • 2





    Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

    – Sumit Deshpande
    Nov 15 '18 at 6:52













  • wont work for <= will result < =

    – styx
    Nov 15 '18 at 6:53














  • 2





    Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

    – Sumit Deshpande
    Nov 15 '18 at 6:52













  • wont work for <= will result < =

    – styx
    Nov 15 '18 at 6:53








2




2





Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

– Sumit Deshpande
Nov 15 '18 at 6:52







Thanks for the suggestion, But this is not going to work in case of a scenario like "number1<=number2" it will end up returning "number1 < = number2" which is not correct

– Sumit Deshpande
Nov 15 '18 at 6:52















wont work for <= will result < =

– styx
Nov 15 '18 at 6:53





wont work for <= will result < =

– styx
Nov 15 '18 at 6:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313758%2fhow-to-add-space-before-and-after-contional-operator-in-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine