0

I'm trying to use a variable inside of an interpolated string, but not having much luck. How would I do this?

var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}

This is what I am trying to achieve:

Console.WriteLine(t); // PRINTS: hello world mike

Is this possible?

For example, suppose I have a method:

public string FormatString(string s) {
      var Now = DateTime.Now;
      return $s;
}

Usage:

Console.WriteLine(FormatString("The time is {Now}"));
5
  • is there no way of storing a variable (desc), and somehow have it interpreted based on contextual ambient variables? Commented Nov 22, 2018 at 2:05
  • @mjwlls, yup that's a duplicate - that's exactly what i'm trying to do Commented Nov 22, 2018 at 2:17
  • Since its not available within the C# language, does anyone know if there is a library? I know its been recommended to use CSharpScript.EvaluateAsync in the other answer, but I need to be able to pass in complex objects in the ambient context, not just string variables. Commented Nov 22, 2018 at 2:34
  • Actually, never mind. CSharpScript.EvaluateAsync works for me, it supports complex global context parameters. Commented Nov 22, 2018 at 2:43
  • weblogs.asp.net/bleroy/… Commented Feb 14, 2020 at 13:48

1 Answer 1

7

You are missing a $

var name = "mike";
var desc = $"hello world {name}"; // this needs be interpolated as well
var t = $"{desc}";
Console.WriteLine(t); // PRINTS: hello world mike

Additional Resources

$ - string interpolation (C# Reference)

The $ special character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolated expressions. When an interpolated string is resolved to a result string, items with interpolated expressions are replaced by the string representations of the expression results. This feature is available in C# 6 and later versions of the language.


Update

but suppose I want to have a variable storing the string with {name} in it. Is there no way to achieve interpolation if its in a variable?

No you would have to use standard String.Format Tokens

var tokenString = "Something {0}";

String.Format(tokenString,someVariable); 

String.Format Method

Converts the value of objects to strings based on the formats specified and inserts them into another string.

Use String.Format if you need to insert the value of an object, variable, or expression into another string. For example, you can insert the value of a Decimal value into a string to display it to the user as a single string:

Composite Formatting

The .NET composite formatting feature takes a list of objects and a composite format string as input. A composite format string consists of fixed text intermixed with indexed placeholders, called format items, that correspond to the objects in the list. The formatting operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.

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

4 Comments

I'm trying to keep desc as a variable. i.e. suppose I want to pass in the string as a variable. Is there a way to interpolate the variable?
You have more details so I'll remove mine as it basically has the same. Looks like a dupe was found.
@Nkosi all good we answered it at the same time, its a sloooow day. Hence the excessive details
Thanks for the feedback guys, really appreciate it. It's too bad there isn't a way to do this using interpolated strings. I can probably do it with string.format, but it aint gonna be pretty

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.