C# 多行字符串变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16107697/
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 variable
提问by ElektroStudios
In .Net (C# and VB.NET) If i have a multiline text like this:
在 .Net(C# 和 VB.NET)中,如果我有这样的多行文本:
__ __ _
\ \ / / | |
\ V /___ _ _ _ __ | | ___ __ _ ___
\ // _ \| | | | '__| | | / _ \ / _` |/ _ \
| | (_) | |_| | | | |___| (_) | (_| | (_) |
\_/\___/ \__,_|_| \_____/\___/ \__, |\___/
__/ |
|___/
Can I set the variable like this?
我可以像这样设置变量吗?
Dim Logo As String = ("
__ __ _
\ \ / / | |
\ V /___ _ _ _ __ | | ___ __ _ ___
\ // _ \| | | | '__| | | / _ \ / _` |/ _ \
| | (_) | |_| | | | |___| (_) | (_| | (_) |
\_/\___/ \__,_|_| \_____/\___/ \__, |\___/
__/ |
|___/ ")
Console.WriteLine(Logo)
... instead of this else:
... 而不是这个:
Console.WriteLine("__ __ ")
Console.WriteLine("\ \ / / | | ")
Console.WriteLine(" \ V /___ _ _ _ __ | | ___ __ _ ___ ")
Console.WriteLine(" \ // _ \| | | | '__| | | / _ \ / _` |/ _ \ ")
Console.WriteLine(" | | (_) | |_| | | | |___| (_) | (_| | (_) |")
Console.WriteLine(" \_/\___/ \__,_|_| \_____/\___/ \__, |\___/ ")
Console.WriteLine(" __/ | ")
Console.WriteLine(" |___/ ")
... or this else :
...或者这个:
Dim Logo As String = ( _
"__ __ _ " & vbNewLine & _
"\ \ / / | | " & vbNewLine & _
" \ V /___ _ _ _ __ | | ___ __ _ ___ " & vbNewLine & _
" \ // _ \| | | | '__| | | / _ \ / _` |/ _ \ " & vbNewLine & _
" | | (_) | |_| | | | |___| (_) | (_| | (_) |" & vbNewLine & _
" \_/\___/ \__,_|_| \_____/\___/ \__, |\___/ " & vbNewLine & _
" __/ | " & vbNewLine & _
" |___/ ")
采纳答案by Geoff
You (initially) marked this as c#, but show VB code. For c#: use the @specifier:
您(最初)将其标记为 c#,但显示了 VB 代码。对于 c#:使用说明@符:
string myText =
@"line 1
line 2
line 3"
Note that if you don't want a blank line at the start of your string, make sure you put the @"on the same line as the first line of your text, as I've done above.
请注意,如果您不想在字符串的开头出现空行,请确保将 与@"文本的第一行放在同一行,就像我上面所做的那样。
For VB.NET, there is no direct support for this, but you can use the nice hack from this answerto get around it:
对于 VB.NET,对此没有直接支持,但您可以使用此答案中的漂亮技巧来绕过它:
Dim s As String = <a>line 1
line 2
line 3</a>.Value
Also consider creating a string resource; you can add line breaks in there (ensure you use shift-enter, per the note in this answer), then load the resource using something similar to
还要考虑创建一个字符串资源;您可以在其中添加换行符(确保使用 shift-enter,根据本答案中的注释),然后使用类似于以下内容的内容加载资源
Dim myString As String = My.Resources.MyString
Update for Visual Studio 2015: Obviously vb.net was the difficult case here, but as of VS2015 it supports multi-line strings in a fashion similar to c# verbatim strings, but without the preceding @.
Visual Studio 2015 更新:显然 vb.net 是这里的困难案例,但从 VS2015 开始,它以类似于 c# 逐字字符串的方式支持多行字符串,但没有前面的@.
Note that the line terminators embedded in the string are the actual line terminators provided by your editor of choice. For VS this is \r\n.
请注意,字符串中嵌入的行终止符是您选择的编辑器提供的实际行终止符。对于 VS,这是\r\n.
Example:
例子:


Source here.
来源在这里。
For the new interpolated stringsintroduced in VS2015 / C# 6, prefix the string with $@in C#:
对于VS2015 / C# 6 中引入的新内插字符串,$@在 C# 中为字符串添加前缀:
string multiline = $@"
[configuration]
name=Fred
age={age}";
In VB.NET, just leave out the @:
在 VB.NET 中,只需省略@:
Dim multiline As String = $"
[configuration]
name=Fred
age={age}"
回答by Dzmitry Martavoi
Yes, you should use @symbol:
是的,你应该使用@符号:
string t = @"t
e
s
t"
回答by Vladimir
In C# you can use raw strings (@), like that:
在 C# 中,您可以使用原始字符串 (@),如下所示:
private string Logo = @"
__ __ _
\ \ / / | |
\ V /___ _ _ _ __ | | ___ __ _ ___
\ // _ \| | | | '__| | | / _ \ / _` |/ _ \
| | (_) | |_| | | | |___| (_) | (_| | (_) |
\_/\___/ \__,_|_| \_____/\___/ \__, |\___/
__/ |
|___/ ";
回答by Captain Skyhawk
Kind of...
的种类...
It's done like this:
它是这样完成的:
Dim logo = " " & vbCrLf & _
"__ __ " & vbCrLf & _
"\ \ / / | | " & vbCrLf & _
etc.
等等。
回答by Sam Leach
Use Verbatim strings.
使用逐字字符串。
The @symbol tells the string constructor to ignore line breaks.
该@符号告诉字符串构造函数忽略换行符。
See MSDNfor more information. String literals vs Verbatim strings
有关更多信息,请参阅MSDN。字符串文字 vs 逐字字符串
For example
例如
string verbatim = @"v
e
r
batim"
Your example
你的榜样
Dim Logo As String = (@"
__ __ _
\ \ / / | |
\ V /___ _ _ _ __ | | ___ __ _ ___
\ // _ \| | | | '__| | | / _ \ / _` |/ _ \
| | (_) | |_| | | | |___| (_) | (_| | (_) |
\_/\___/ \__,_|_| \_____/\___/ \__, |\___/
__/ |
|___/ ")
Console.WriteLine(Logo)

