C
C#2y ago
JoHecht

String interpolation on an existing String

I receive a string out of a Database. Say like "Hello {Name}" which is safed in the variable x I know want to use string interpolation on that string I know this works
string Name = "Peter"
string hello = $"Hello {Name}";
string Name = "Peter"
string hello = $"Hello {Name}";
Result would be hello Peter What I want to do is this
string Name = "Peter"
string hello = $"x";
string Name = "Peter"
string hello = $"x";
With the same result Is this even possible?
4 Replies
Aaron
Aaron2y ago
you can use string.Format
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Success
string s = "{0} was first, {1} was second";
string.Format(s, "hello", "world")
string s = "{0} was first, {1} was second";
string.Format(s, "hello", "world")
Result: string
hello was first, world was second
hello was first, world was second
Compile: 438.734ms | Execution: 29.681ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
forgot you need explicit numbers for format but you can't grab the values of random variables
JoHecht
JoHechtOP2y ago
thank u appreciate it
Want results from more Discord servers?
Add your server