1

Following interpolated string formation works well:

string BG="7263-2323";
string PG="2983-2323";

string interpolatedString = $"{BG};{PG}";
Console.WriteLine(interpolatedString);

Result in variable interpolatedString:

7263-2323;2983-2323

But, the problem is that I store interpolatedString in database, then it does not display values for BG and PG... Instead it displays string as it is:

$\"{BG};{PG}\"

How can I solve this issue? Any idea?

3
  • 2
    How do you store them, show the code where you insert the data. Also, why do you store multiple things in the same colunm at all? Why do you even store the literal string at all? You only need the BG and the PG values. Commented Mar 29, 2017 at 13:12
  • 4
    String interpolation is a compiler feature. After compilation the application knows nothing about variables or anything. You can do things with numbered placeholders since you know the variables beforehand. Commented Mar 29, 2017 at 13:13
  • You need to write your own interpolated strings parser to fill in properties/variables at runtime. Commented Mar 29, 2017 at 13:15

1 Answer 1

2

You can't do that. Interpolated strings are effectively just syntactic sugar for using string.Format. Instead you would need to store your strings like this:

Hello {0}, welcome to my app

And then use string.Format:

var format = "Hello {0}, welcome to my app";
var output = string.format(format, "Bob");

Alternatively you could roll your own, for example:

var format = "Hello {name}, welcome to my app";
var output = format.Replace("{name}", "Bob");

Note that my example here isn't particularly efficient so you may want to use something like StringBuilder if you're doing a lot of this.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.