mrd
mrd

Reputation: 2183

Interpolated string in database

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?

Upvotes: 1

Views: 480

Answers (1)

DavidG
DavidG

Reputation: 119056

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.

Upvotes: 2

Related Questions