可能是重复问题:
VB.NET中的多行字符串
在c#中,你可以这样写:
string s = @"hello there mister";
VB.NET是否有类似的方法而不涉及字符串连接?我想能够在两个双引号之间粘贴多行文本。但我不相信VB.NET支持这一点。
可能是重复问题:
VB.NET中的多行字符串
在c#中,你可以这样写:
string s = @"hello there mister";
VB.NET是否有类似的方法而不涉及字符串连接?我想能够在两个双引号之间粘贴多行文本。但我不相信VB.NET支持这一点。
编辑:VS2015及以上版本
现在你可以通过以下方式来实现多行字符串:
Dim text as String = "
This
Is
Multiline
Text!"
VB .NET 中没有多行字符串文本 - 最接近的方式是使用拼接来实现(不使用 LINQ)。
在 VS2010 之前:
Dim x = "some string" & vbCrlf & _
"the rest of the string"
2010年后发布的内容:
Dim x = "some string" & vbCrlf &
"the rest of the string"
XML/LINQ 的技巧是:
Imports System.Core
Imports System.XML
Imports System.XML.Linq
Dim x As String = <a>Some code
and stuff</a>.Value
但这会限制你在 <a></a>
块中可以放置哪些字符,因为涉及到 XML 语义。如果你需要使用特殊字符,请将文本包含在标准的 CDATA
块中:
Dim x As String = <a><![CDATA[Some code
& stuff]]></a>.Value
Dim s As String = <a>hello
there
mister</a>.Value
或者将您的字符串放入项目资源中。
我不确定这是否是最好的做法,但我认为没有等价的运算符。
Dim myString As String =
"Hello" & Environment.NewLine & "there" & Environment.NewLine & "mister"
我认为Environement.NewLine
会根据操作系统自动选择正确的换行符。
编辑:我刚刚看到您想要直接在代码中插入多行文本,因此还有另一种可能的解决方案:
您仍然需要使用字符串引号和逗号,但是以下是示例:
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
这是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."
dim multiline = $"Multiliine String!"
- Luke T O'Brien