How do I interpolate strings?

梦想与她 提交于 2019-11-26 16:24:26
Darin Dimitrov
string mystr = string.Format("This is {0}overflow", strVar);

And you could also use named parameters instead of indexes.

Ashtonian

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

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();
Sakal

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.

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.

Matthew Abbott

Use string.Format:

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

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.

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";

There's one more way to implement placeholders with string.Replace, oddly helps in certain situations:

mystr = mystr.Replace("%soverflow", strVar);

You can use the following way

String interpolation

The $ special character identifies a string literal as an interpolated string. e.g.

string name = "Mark";
string surname = "D'souza";
WriteLine($"Name :{name} Surname :{surname}" );//Name :Mark Surname :D'souza  

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.

String.Format

Use String.Format if you need to insert the value of an object, variable, or expression into another string.E.g.

WriteLine(String.Format("Name: {0}, Surname : {1}", name, surname));

Use:

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

You can accomplish this with Expansive: https://github.com/anderly/Expansive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!