VB.NET 是否具有与 c# 等效的多行字符串声明语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11954701/
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
Does VB.NET have a multi-line string declaration syntax equivalent to c#?
提问by oscilatingcretin
Possible Duplicate:
Multiline strings in VB.NET
可能的重复:
VB.NET 中的多行字符串
In c#, you can be all like:
在 c# 中,你可以像:
string s = @"hello there mister";
Does VB.NET have something similar that doesn't involve string concatenation? I'd like to be able to paste multi-line text in between two double quotes. Somehow, I don't believe VB.NET supports this.
VB.NET 是否有类似的不涉及字符串连接的东西?我希望能够在两个双引号之间粘贴多行文本。不知何故,我不相信 VB.NET 支持这一点。
采纳答案by Matt Razza
EDIT: VS2015 ONWARDS
编辑:VS2015 以后
YOU CAN NOW HAVE MULTILINE STRINGS IN VS2015 BY WRITING THEM LIKE SO:
你现在可以在 VS2015 中通过这样写来获得多行字符串:
Dim text as String = "
This
Is
Multiline
Text!"
There is no multi-line string literal in VB .NET - the closest thing you can get (without using LINQ) is a multi-line statement with concatenation.
在 VB .NET 中没有多行字符串文字——你能得到的最接近的东西(不使用 LINQ)是带有连接的多行语句。
Prior to VS2010:
在 VS2010 之前:
Dim x = "some string" & vbCrlf & _
"the rest of the string"
Post 2010:
2010 年后:
Dim x = "some string" & vbCrlf &
"the rest of the string"
The XML/LINQ trick is:
XML/LINQ 技巧是:
Imports System.Core
Imports System.XML
Imports System.XML.Linq
Dim x As String = <a>Some code
and stuff</a>.Value
But this limits what characters you can place inside the <a></a>block because of XML semantics. If you need to use special characters wrap the text in a standard CDATAblock:
但是<a></a>由于 XML 语义,这限制了可以在块中放置的字符。如果您需要使用特殊字符将文本包装在标准CDATA块中:
Dim x As String = <a><![CDATA[Some code
& stuff]]></a>.Value
回答by Pacane
I dont know if it's the best way of doing this but I don't think there's an equivalent operator.
我不知道这是否是最好的方法,但我认为没有等效的运算符。
Dim myString As String =
"Hello" & Environment.NewLine & "there" & Environment.NewLine & "mister"
I think the Environement.NewLinetakes the correct line feed, depending on the OS.
我认为Environement.NewLine需要正确的换行符,具体取决于操作系统。
EDIT: I've just read that you want to insert text multiline directly in the code, so there's another possible solution:
编辑:我刚刚读到您想直接在代码中插入文本多行,所以还有另一种可能的解决方案:
You have to use string quotations still, and commas, but here it is
您仍然必须使用字符串引号和逗号,但这里是
Dim myList as new List(of String) (new String(){
"Hello",
"there",
"mister"
})
Dim result as String
For Each bob as String In myList
result += bob & Environment.NewLine
next
回答by user1598203
No, but you can use a xml trick like this:
不,但您可以使用这样的 xml 技巧:
Dim s As String = <a>hello
there
mister</a>.Value
or put your string in a project resource.
或将您的字符串放在项目资源中。
回答by amdmax
This is what MSDN recommends http://msdn.microsoft.com/en-us/library/5chcthbw(v=vs.80).aspx
这是 MSDN 推荐的 http://msdn.microsoft.com/en-us/library/5chcthbw(v=vs.80).aspx
MyString = "This is the first line of my string." & VbCrLf & _ "This is the second line of my string." & VbCrLf & _ "This is the third line of my string."
MyString = "这是我的字符串的第一行。" & VbCrLf & _ “这是我的字符串的第二行。” & VbCrLf & _ “这是我的字符串的第三行。”

