Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interpolate strings?

I want to do the following in C# (coming from a Python background):

strVar = "stack"
mystr  = "This is %soverflow" % (strVar)

How do I replace the token inside the string with the value outside of it?

like image 974
sazr Avatar asked Feb 19 '12 22:02

sazr


People also ask

What does string interpolation do?

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

Is there string interpolation in Python?

Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix f . This new way of formatting strings is powerful and easy to use. It provides access to embedded Python expressions inside string constants.


9 Answers

This has been added as of C# 6.0 (Visual Studio 2015+).

Example:

var planetName = "Bob";
var myName = "Ford"; 
var formattedStr = $"Hello planet {planetName}, my name is {myName}!";
// formattedStr should be "Hello planet Bob, my name is Ford!"

This is syntactic sugar for:

var formattedStr = String.Format("Hello planet {0}, my name is {1}!", planetName, myName);

Additional Resources:

String Interpolation for C# (v2) Discussion

C# 6.0 Language Preview

like image 194
Ashtonian Avatar answered Oct 05 '22 22:10

Ashtonian


string mystr = string.Format("This is {0}overflow", strVar);

And you could also use named parameters instead of indexes.

like image 39
Darin Dimitrov Avatar answered Oct 05 '22 21:10

Darin Dimitrov


You can use string.Format to drop values into strings:

private static readonly string formatString = "This is {0}overflow";
...
var strVar = "stack";
var myStr = string.Format(formatString, "stack");

An alternative is to use the C# concatenation operator:

var strVar = "stack";
var myStr = "This is " + strVar + "overflow";

If you're doing a lot of concatenations use the StringBuilder class which is more efficient:

var strVar = "stack";
var stringBuilder = new StringBuilder("This is ");
for (;;)
{
    stringBuilder.Append(strVar); // spot the deliberate mistake ;-)
}
stringBuilder.Append("overflow");
var myStr = stringBuilder.ToString();
like image 34
David Clarke Avatar answered Oct 05 '22 22:10

David Clarke


If you currently use Visual Studio 2015 with C# 6.0, try the following:

var strVar = "stack";

string str = $"This is {strVar} OverFlow";

that feature is called string interpolation.

like image 28
Sakal Avatar answered Oct 05 '22 20:10

Sakal


C# 6.0

string mystr = $"This is {strVar}overflow";
like image 30
Sylvain Rodrigue Avatar answered Oct 05 '22 21:10

Sylvain Rodrigue


There is no operator for that. You need to use string.Format.

string strVar = "stack";
string mystr  = string.Format("This is {0}soverflow", strVar);

Unfortunately string.Format is a static method, so you can't simply write "This is {0}soverflow".Format(strVar). Some people have defined an extension method, that allows this syntax.

like image 35
CodesInChaos Avatar answered Oct 05 '22 20:10

CodesInChaos


Use string.Format:

string mystr = string.Format("This is {0}overflow", "stack");
like image 37
Matthew Abbott Avatar answered Oct 05 '22 20:10

Matthew Abbott


You should be using String.Format(). The syntax is a bit different, numerical placeholders are used instead.

Example:

String.Format("item {0}, item {1}", "one", "two")

Have a look at http://msdn.microsoft.com/en-us/library/system.string.format.aspx for more details.

like image 40
Bruno Silva Avatar answered Oct 05 '22 22:10

Bruno Silva


You have 2 options. You can either use String.Format or you can use the concatenation operator.

String newString = String.Format("I inserted this string {0} into this one", oldstring);

OR

String newString = "I inserted this string " + oldstring + " into this one";
like image 41
DwayneAllen Avatar answered Oct 05 '22 21:10

DwayneAllen