7

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?

3
  • 3
    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. Commented Nov 19, 2018 at 23:35
  • 4
    Stop trying to do everything in one line, if you broke this apart you would know the problem Commented Nov 19, 2018 at 23:42
  • Thank Glorin, nice shot. And could you suggest another way for this task without such bad readability - to involve interim variable? Commented Nov 19, 2018 at 23:45

1 Answer 1

13

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

Sign up to request clarification or add additional context in comments.

7 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 that's fair enough, however you were just missing a $
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 its hard to know without seeing the full code, though for readability sometimes (most of the time) simple is better. Don't let anyone ever fault you for breaking things apart and making things more verbose. Its easier to read line by line then to understand a complex nested statements. Though, If you have code that works, may i suggest CodeReview on stackExchange
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.