Nested string interpolation
I've faced an issue with nested string interpolation in C# 6.
For example, there is a string:
string test = "StartText MiddleText1 MiddleText2 EndText";
If I want to apply ToUpper() method for MiddleText1 only, I can do this way:
string test = $@"StartText {"MiddleText1".ToUpper()} MiddleText2 EndText";
But what if I want to apply a string method, for example Replace() for this part of string:
{"Middletext1".ToUpper()} MiddleText2
I expected that something like this will work:
string test = $@"StartText {"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
But this syntax is wrong - I've tried a lot variations, played with quotas but I couldn't get correct syntax for this purpose.
I'd wish to not split the string in a different parts. Is there a way to solve it using interpolation feature only?
c# string interpolation
add a comment |
I've faced an issue with nested string interpolation in C# 6.
For example, there is a string:
string test = "StartText MiddleText1 MiddleText2 EndText";
If I want to apply ToUpper() method for MiddleText1 only, I can do this way:
string test = $@"StartText {"MiddleText1".ToUpper()} MiddleText2 EndText";
But what if I want to apply a string method, for example Replace() for this part of string:
{"Middletext1".ToUpper()} MiddleText2
I expected that something like this will work:
string test = $@"StartText {"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
But this syntax is wrong - I've tried a lot variations, played with quotas but I couldn't get correct syntax for this purpose.
I'd wish to not split the string in a different parts. Is there a way to solve it using interpolation feature only?
c# string interpolation
2
Just shooting from the hip here...string test = $@"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
... that said, this looks just painful to read and really defeats the purpose of string interpolation.
– Glorin Oakenfoot
Nov 19 '18 at 23:35
2
Stop trying to do everything in one line, if you broke this apart you would know the problem
– TheGeneral
Nov 19 '18 at 23:42
Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable?
– Vladimir
Nov 19 '18 at 23:45
add a comment |
I've faced an issue with nested string interpolation in C# 6.
For example, there is a string:
string test = "StartText MiddleText1 MiddleText2 EndText";
If I want to apply ToUpper() method for MiddleText1 only, I can do this way:
string test = $@"StartText {"MiddleText1".ToUpper()} MiddleText2 EndText";
But what if I want to apply a string method, for example Replace() for this part of string:
{"Middletext1".ToUpper()} MiddleText2
I expected that something like this will work:
string test = $@"StartText {"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
But this syntax is wrong - I've tried a lot variations, played with quotas but I couldn't get correct syntax for this purpose.
I'd wish to not split the string in a different parts. Is there a way to solve it using interpolation feature only?
c# string interpolation
I've faced an issue with nested string interpolation in C# 6.
For example, there is a string:
string test = "StartText MiddleText1 MiddleText2 EndText";
If I want to apply ToUpper() method for MiddleText1 only, I can do this way:
string test = $@"StartText {"MiddleText1".ToUpper()} MiddleText2 EndText";
But what if I want to apply a string method, for example Replace() for this part of string:
{"Middletext1".ToUpper()} MiddleText2
I expected that something like this will work:
string test = $@"StartText {"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
But this syntax is wrong - I've tried a lot variations, played with quotas but I couldn't get correct syntax for this purpose.
I'd wish to not split the string in a different parts. Is there a way to solve it using interpolation feature only?
c# string interpolation
c# string interpolation
asked Nov 19 '18 at 23:31
VladimirVladimir
129211
129211
2
Just shooting from the hip here...string test = $@"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
... that said, this looks just painful to read and really defeats the purpose of string interpolation.
– Glorin Oakenfoot
Nov 19 '18 at 23:35
2
Stop trying to do everything in one line, if you broke this apart you would know the problem
– TheGeneral
Nov 19 '18 at 23:42
Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable?
– Vladimir
Nov 19 '18 at 23:45
add a comment |
2
Just shooting from the hip here...string test = $@"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
... that said, this looks just painful to read and really defeats the purpose of string interpolation.
– Glorin Oakenfoot
Nov 19 '18 at 23:35
2
Stop trying to do everything in one line, if you broke this apart you would know the problem
– TheGeneral
Nov 19 '18 at 23:42
Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable?
– Vladimir
Nov 19 '18 at 23:45
2
2
Just shooting from the hip here...
string test = $@"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
... that said, this looks just painful to read and really defeats the purpose of string interpolation.– Glorin Oakenfoot
Nov 19 '18 at 23:35
Just shooting from the hip here...
string test = $@"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
... that said, this looks just painful to read and really defeats the purpose of string interpolation.– Glorin Oakenfoot
Nov 19 '18 at 23:35
2
2
Stop trying to do everything in one line, if you broke this apart you would know the problem
– TheGeneral
Nov 19 '18 at 23:42
Stop trying to do everything in one line, if you broke this apart you would know the problem
– TheGeneral
Nov 19 '18 at 23:42
Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable?
– Vladimir
Nov 19 '18 at 23:45
Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable?
– Vladimir
Nov 19 '18 at 23:45
add a comment |
1 Answer
1
active
oldest
votes
Stop trying to do everything in one line is my suggestion
The following is the answer
var middle = "MiddleText1";
middle = middle.ToUpper();
var middle2 = $"{middle} MiddleText2";
middle2 = middle2.Replace("x", "y");
string test = $"StartText {middle2} EndText";
Which, when you add it all together.
string test = $"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
In short, you were just missing a $
However, Even this is messy as i am not sure what all the replaces are for, where this text comes from, and what the issue is you are trying to solve
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
1
@Vladimir that's fair enough, however you were just missing a$
– TheGeneral
Nov 19 '18 at 23:54
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
1
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
1
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
|
show 3 more comments
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%2f53384153%2fnested-string-interpolation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Stop trying to do everything in one line is my suggestion
The following is the answer
var middle = "MiddleText1";
middle = middle.ToUpper();
var middle2 = $"{middle} MiddleText2";
middle2 = middle2.Replace("x", "y");
string test = $"StartText {middle2} EndText";
Which, when you add it all together.
string test = $"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
In short, you were just missing a $
However, Even this is messy as i am not sure what all the replaces are for, where this text comes from, and what the issue is you are trying to solve
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
1
@Vladimir that's fair enough, however you were just missing a$
– TheGeneral
Nov 19 '18 at 23:54
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
1
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
1
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
|
show 3 more comments
Stop trying to do everything in one line is my suggestion
The following is the answer
var middle = "MiddleText1";
middle = middle.ToUpper();
var middle2 = $"{middle} MiddleText2";
middle2 = middle2.Replace("x", "y");
string test = $"StartText {middle2} EndText";
Which, when you add it all together.
string test = $"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
In short, you were just missing a $
However, Even this is messy as i am not sure what all the replaces are for, where this text comes from, and what the issue is you are trying to solve
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
1
@Vladimir that's fair enough, however you were just missing a$
– TheGeneral
Nov 19 '18 at 23:54
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
1
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
1
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
|
show 3 more comments
Stop trying to do everything in one line is my suggestion
The following is the answer
var middle = "MiddleText1";
middle = middle.ToUpper();
var middle2 = $"{middle} MiddleText2";
middle2 = middle2.Replace("x", "y");
string test = $"StartText {middle2} EndText";
Which, when you add it all together.
string test = $"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
In short, you were just missing a $
However, Even this is messy as i am not sure what all the replaces are for, where this text comes from, and what the issue is you are trying to solve
Stop trying to do everything in one line is my suggestion
The following is the answer
var middle = "MiddleText1";
middle = middle.ToUpper();
var middle2 = $"{middle} MiddleText2";
middle2 = middle2.Replace("x", "y");
string test = $"StartText {middle2} EndText";
Which, when you add it all together.
string test = $"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
In short, you were just missing a $
However, Even this is messy as i am not sure what all the replaces are for, where this text comes from, and what the issue is you are trying to solve
edited Nov 20 '18 at 0:06
answered Nov 19 '18 at 23:46


TheGeneralTheGeneral
28.4k63365
28.4k63365
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
1
@Vladimir that's fair enough, however you were just missing a$
– TheGeneral
Nov 19 '18 at 23:54
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
1
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
1
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
|
show 3 more comments
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
1
@Vladimir that's fair enough, however you were just missing a$
– TheGeneral
Nov 19 '18 at 23:54
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
1
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
1
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
This is good approach, but original text is large and I need to make many similar method invocations, that's why I tried to get one-line solution to avoid creating a lot of additional variables.
– Vladimir
Nov 19 '18 at 23:54
1
1
@Vladimir that's fair enough, however you were just missing a
$
– TheGeneral
Nov 19 '18 at 23:54
@Vladimir that's fair enough, however you were just missing a
$
– TheGeneral
Nov 19 '18 at 23:54
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
Actually this question came from work with some kind of "html page builder". It uses string extension methods to create html page from some volume of text, returning inner strings embedded in different html tags. For example, it will return some parts in <td> tag, then result embedded in <tr>, then result in <table> tag etc. Now I see It will be really hard readable. Maybe you could suggest another approach?
– Vladimir
Nov 20 '18 at 0:04
1
1
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
@Vladimir codereview.stackexchange.com
– TheGeneral
Nov 20 '18 at 0:18
1
1
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
@Vladimir: Generally the only thing you get for squeezing a lot of operations into a single line, is a horrible time debugging. Because you can not figure out wich of those 3-10 operations is even causing the issue. Do not be to worried about performance. The JiT is pretty good at cutting out dead code and adding/removing Temporary variables in Release builds to improove performance.
– Christopher
Nov 20 '18 at 0:48
|
show 3 more comments
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%2f53384153%2fnested-string-interpolation%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
2
Just shooting from the hip here...
string test = $@"StartText {$"{"MiddleText1".ToUpper()} MiddleText2".Replace("x", "y")} EndText";
... that said, this looks just painful to read and really defeats the purpose of string interpolation.– Glorin Oakenfoot
Nov 19 '18 at 23:35
2
Stop trying to do everything in one line, if you broke this apart you would know the problem
– TheGeneral
Nov 19 '18 at 23:42
Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable?
– Vladimir
Nov 19 '18 at 23:45