How to add space before and after contional operator in string?
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
add a comment |
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
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
add a comment |
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
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
c# string c#-4.0
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
Probably, a better choices*
instead ofs?
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
add a comment |
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;
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Probably, a better choices*
instead ofs?
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
add a comment |
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
Probably, a better choices*
instead ofs?
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
add a comment |
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
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
edited Nov 15 '18 at 8:20
answered Nov 15 '18 at 6:54
SweeperSweeper
69.2k1074140
69.2k1074140
Probably, a better choices*
instead ofs?
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
add a comment |
Probably, a better choices*
instead ofs?
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
add a comment |
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;
}
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
add a comment |
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;
}
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
add a comment |
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;
}
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;
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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