C# 如何在 VB.NET 中逐字逐字地执行字符串文字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13155449/
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
How to do a verbatim string literal in VB.NET?
提问by CJ7
How do you do a verbatimstring literal in VB.NET?
你如何在 VB.NET 中逐字逐字地处理字符串?
This is achieved in C# as follows:
这是在 C# 中实现的,如下所示:
String str = @"c:\folder1\file1.txt";
This means that the backslashes are treated literally and not as escape characters.
这意味着反斜杠按字面处理,而不是转义字符。
How is this achieved in VB.NET?
这是如何在 VB.NET 中实现的?
采纳答案by Steve
All string literals in VB.NET are verbatim string literals. Simply write
VB.NET 中的所有字符串文字都是逐字字符串文字。简单地写
Dim str As String = "c:\folder1\file1.txt"
VB.NET doesn't support inline control characters. So backslashes are always interpreted literally.
VB.NET 不支持内联控制字符。所以反斜杠总是按字面解释。
The only character that needs to be escaped is the double quotation mark, which is escaped by doubling it, as you do in C#
唯一需要转义的字符是双引号,它通过加倍转义,就像在 C# 中所做的那样
Dim s As String = """Ahoy!"" cried the captain." ' "Ahoy!" cried the captain.
回答by Jon Skeet
VB doesn't treat \as an escape character anyway, so you can just write the string as a normal literal:
VB\无论如何都不会将其视为转义字符,因此您可以将字符串写为普通文字:
Dim str = "c:\folder1\file1.txt"
As far as I'm aware, VB doesn't have any way of achieving the other major goal of verbatim string literals, that of allowing multiple lines - you have to use VbCrLffor that, I believe. (Or Environment.NewLineof course - it depends on your requirements. Sometimes you want the system-specific line separator; sometimes you want a specific one as required by a particular protocol etc.)
据我所知,VB 没有任何方法可以实现逐字字符串文字的另一个主要目标,即允许多行 -我相信你必须使用VbCrLf它。(或者Environment.NewLine当然 - 这取决于您的要求。有时您需要系统特定的行分隔符;有时您需要特定协议等要求的特定行分隔符)
EDIT: Newer versions of VB support multiple lines in string literals
回答by Carlos Quintanilla
When in doubt look at this comparison page: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html
如有疑问,请查看此比较页面:http: //www.harding.edu/fmccown/vbnet_csharp_comparison.html
VB.NET
网络
'No string literal operator
Dim filename As String = "c:\temp\x.dat"
C#
C#
// String literal
string filename = @"c:\temp\x.dat";
string filename = "c:\temp\x.dat";
回答by JJS
@MarkJ already pointed this out in @Jon Skeet's post.
@MarkJ 已经在 @Jon Skeet 的帖子中指出了这一点。
VB.Net supports this abominationfeature, if you absolutely need to use a verbatim via an inline XML Literal.
VB.Net 支持这种令人厌恶的功能,如果您绝对需要通过内联 XML 文字使用逐字逐字。
Consider Caching the String! Don't evaluate this every time...
考虑缓存字符串!不要每次都评价这个...
Imports System.Xml.Linq
Dim cmdText as String = <![CDATA[
SELECT
Field1, Field2, Field3
FROM table
WHERE Field1 = 1
]]>.Value
[edit 2015-Jan-5]
[编辑 2015 年 1 月 5 日]
VB14 / VS2015 supports multi-line strings without any shenanigans.
VB14 / VS2015 支持多行字符串,没有任何恶作剧。
Dim cmdText as String = "
SELECT
Field1, Field2, Field3
FROM table
WHERE Field1 = 1"
回答by Randall
VB XML literals unfortunately will not work in a .vbhtml razor page. Hopefully that will change in the next release.
不幸的是,VB XML 文字在 .vbhtml razor 页面中不起作用。希望在下一个版本中会有所改变。
回答by Aave
VB.NET do not recognize "\" as an escape character. But, maybe you may use further solution (take into account, that it's works slowly than concatenation, e.g.):
VB.NET 不能将“\”识别为转义字符。但是,也许您可以使用进一步的解决方案(请注意,它的工作速度比串联慢,例如):
Dim s As String = Regex.Unescape("c:\folder1\file1.txt\nc:\folder1\file2.txt\nc:\folder1\file3.txt")
In this case, string "s" contains three lines. Symbol "\" is protect next "\" from regex method Unescape(), that's why it repeat twice each time.
在这种情况下,字符串“s”包含三行。符号“\”保护下一个“\”不受正则表达式方法 Unescape() 的影响,这就是它每次重复两次的原因。
"\n" is a C#-like "new line" special character. You also may use "\t" (tab), and so on.
“\n”是一个类似于 C# 的“换行”特殊字符。您也可以使用“\t”(制表符)等。
回答by qianzuijiuren
Dim sourceText As String =
<string>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports Microsoft.Win32
Imports System.Linq
Imports System.Text
Imports Roslyn.Compilers
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports Roslyn.Compilers.VisualBasic
Namespace HelloWorld
Module Program
Sub Main(args As String())
Console.WriteLine("Hello, World!")
End Sub
End Module
End Namespace
</string>

