C# 从变量添加文本的多行字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16195603/
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
Multiline string with added text from variables
提问by enb081
I am aware this will work:
我知道这会起作用:
string multiline_text = @"this is a multiline text
this is line 1
this is line 2
this is line 3";
How can I make the following work:
我怎样才能使以下工作:
string a1 = " line number one";
string a2 = " line number two";
string a3 = " line number three";
string multiline_text = @"this is a multiline text
this is " + a1 + "
this is " + a2 + "
this is " + a3 + ";
Is it possible without splitting the string into several substring, one for each line?
是否可以不将字符串拆分为多个子字符串,每行一个?
采纳答案by Jon Skeet
One option is to use string formatting instead. Before C# 6:
一种选择是改用字符串格式。在 C# 6 之前:
string pattern = @"this is a multiline text
this is {0}
this is {1}
this is {2}";
string result = string.Format(pattern, a1, a2, a3);
With C# 6, you can use an interpolated verbatim string literal:
使用 C# 6,您可以使用内插逐字字符串文字:
string pattern = $@"this is a multiline text
this is {a1}
this is {a2}
this is {a3}";
Note that $@has to be exactly that - if you try to use @$, it won't compile.
请注意,$@必须完全如此 - 如果您尝试使用@$,它将无法编译。
回答by Chris Dixon
You can gain readability like this from the StringBuilderclass:
您可以从StringBuilder类中获得这样的可读性:
StringBuilder sb = new StringBuilder();
sb.AppendLine("this is a multiline");
sb.AppendLine("this is " + a1); // etc
var result = sb.ToString();
回答by Sam I am says Reinstate Monica
For some reason c# doesn't support multi-line text like that. the closest you'll get is:
出于某种原因,c# 不支持这样的多行文本。最接近的是:
string a1 = " line number one";
string a2 = " line number two";
string a3 = " line number three";
string multiline_text = @"this is a multiline text" +
"this is " + a1 +
"this is " + a2 +
"this is " + a3;
回答by mattytommo
Although string.Formatis better practice, to do what you're trying to achieve, just add the extra @s at the end of each line:
虽然string.Format是更好的做法,但要完成您想要实现的目标,只需@在每行的末尾添加额外的s:
string multiline_text = @"this is a multiline text
this is " + a1 + @"
this is " + a2 + @"
this is " + a3 + @"";
You were also missing a last "before the ending semi colon.
您还缺少"结尾分号之前的最后一个。
回答by Mike Perrenoud
Another possible solution that goes along the lines of Jon Skeet's answer is this:
与 Jon Skeet 的回答一致的另一个可能的解决方案是:
string result = string.Format(
"this is a multiline text{0}this is {1}{0}this is {2}{0}this is {3}",
Environment.NewLine, a1, a2, a3);
and so basically you're inserting a new line where you want one with {0}. It makes the string a little more abstract because of that, but the benefit of this solution is that it's fully encapsulated within the string.Formatmethod. It's probably not all that relevant -but worth mentioning.
所以基本上你是在你想要的地方插入一个新行{0}。因此,它使字符串更加抽象,但此解决方案的好处是它完全封装在string.Format方法中。这可能不是那么重要——但值得一提。
回答by Ondrej Petrzilka
With Visual Studio 2015, you can write:
使用 Visual Studio 2015,您可以编写:
string multiline_text = $@"this is a multiline text
this is {a1}
this is {a2}
this is {a3}";
String.Format will be used by compiler (like in Jons answer), but it's easier to read.
String.Format 将由编译器使用(就像在 Jons 的回答中一样),但它更容易阅读。
回答by Dennis Allen
For fun, you can also use an array join technique to get something readable and control indenting. Sometimes the multi-line template forcing you to fully left align is unsightly...
为了好玩,您还可以使用数组连接技术来获得可读性和控制缩进。有时强制您完全左对齐的多行模板不雅观......
string a1 = "London", a2 = "France", a3 = "someone's underpants";
string result = string.Join(Environment.NewLine, new[] {
$"this is {a1}", // interpolated strings look nice
"this is " + a2, // you can concat if you can't interpolate
$"this is {a3}" // these in-line comments can be nice too
});
if you must format, wrap the join for a tidy result instead of individual lines.
如果您必须格式化,请包装连接以获得整洁的结果,而不是单独的行。

