C# 如何插入字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9354154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:00:43  来源:igfitidea点击:

How do I interpolate strings?

c#stringstring-interpolation

提问by sazr

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

我想在 C# 中执行以下操作(来自 Python 背景):

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

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

如何用字符串外部的值替换字符串内部的标记?

采纳答案by Darin Dimitrov

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

And you could also use named parametersinstead of indexes.

你也可以使用命名参数而不是索引。

回答by CodesInChaos

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

没有运营商。您需要使用string.Format.

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

Unfortunately string.Formatis 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.

不幸的string.Format是,它是一个静态方法,因此您不能简单地编写"This is {0}soverflow".Format(strVar). 有些人定义了一个扩展方法,允许使用这种语法。

回答by Matthew Abbott

Use string.Format:

使用string.Format

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

回答by Phillip Ngan

Use:

用:

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

回答by Bruno Silva

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

你应该使用String.Format(). 语法有点不同,而是使用数字占位符。

Example:

例子:

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

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

有关更多详细信息,请查看http://msdn.microsoft.com/en-us/library/system.string.format.aspx

回答by DwayneAllen

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

您有 2 个选择。您可以使用 String.Format,也可以使用连接运算符。

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

OR

或者

String newString = "I inserted this string " + oldstring + " into this one";

回答by David Clarke

You can use string.Formatto drop values into strings:

您可以使用string.Format将值放入字符串中:

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:

另一种方法是使用 C# 连接运算符:

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

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

如果您要进行大量连接,请使用StringBuilder效率更高的类:

var strVar = "stack";
var stringBuilder = new StringBuilder("This is ");
for (;;)
{
    stringBuilder.Append(strVar); // spot the deliberate mistake ;-)
}
stringBuilder.Append("overflow");
var myStr = stringBuilder.ToString();

回答by anderly

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

您可以使用 Expansive 完成此操作:https: //github.com/anderly/Expansive

回答by Ashtonian

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

这已从 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# 的字符串插值 (v2) 讨论

C# 6.0 Language Preview

C# 6.0 语言预览

回答by Sakal

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

如果您当前使用的是带有 C# 6.0 的 Visual Studio 2015,请尝试以下操作:

var strVar = "stack";

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

that feature is called string interpolation.

该功能称为字符串插值。