vb.net 如何在 Visual Basic .NET 中指定长字符串文字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18510247/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:51:04  来源:igfitidea点击:

How to specify long string literals in Visual Basic .NET?

vb.net

提问by miroxlav

Is there a way to conveniently store long string literal in Visual Basic source? I'm composing a console application with --helpprintout let's say 20 lines long.

有没有一种方法可以方便地在 Visual Basic 源代码中存储长字符串文字?我正在编写一个带有--help打印输出的控制台应用程序,假设有 20 行长。

Most welcome would be to have raw text area somewhere in source code where I can manage output text 1:1. Many languages supply HEREDOC functionality. In the VB, I couldn't find it. But maybe this could be tricked somehow via LINQ (XML)?

最受欢迎的是在源代码中的某个地方有原始文本区域,我可以在那里以 1:1 的比例管理输出文本。许多语言提供 HEREDOC 功能。在VB中,我找不到它。但也许这可以通过 LINQ (XML) 以某种方式被欺骗?

Thank you for good tips in advance!

感谢您提前提供好的提示!

回答by Steven Doggart

Update:

更新:

In VB.NET 14, which comes with Visual Studio 2015, all string literals support multiple lines. In VB.NET 14, all string literals work like verbatimstring literals in C#. For instance:

在 Visual Studio 2015 附带的 VB.NET 14 中,所有字符串文字都支持多行。在 VB.NET 14 中,所有字符串文字都像C# 中的逐字字符串文字一样工作。例如:

Dim longString = "line 1
line 2
line 3"


Original Answer:

原答案:

c# has a handy multi-line-string-literal syntax using the @symbol (called verbatim strings), but unfortunately VB.NET does not have a direct equivalent to that (this is no longer true--see update above). There are several other options, however, that you may still find helpful.

c# 有一个使用@符号的方便的多行字符串文字语法(称为逐字字符串),但不幸的是 VB.NET 没有直接等效的语法(这不再是真实的 - 请参阅上面的更新)。但是,还有其他几个选项可能对您有帮助。

Option 1: Simple Concatenation

选项 1:简单串联

Dim longString As String =
    "line 1" & Environment.NewLine &
    "line 2" & Environment.NewLine &
    "line 3"

Or the less .NET purist may choose:

或者较少的 .NET 纯粹主义者可以选择:

Dim longString As String =
    "line 1" & vbCrLf &
    "line 2" & vbCrLf &
    "line 3"

Option 2: String Builder

选项 2:字符串生成器

Dim builder As New StringBuilder()
builder.AppendLine("line 1")
builder.AppendLine("line 2")
builder.AppendLine("line 3")
Dim longString As String = builder.ToString()

Option 3: XML

选项 3:XML

Dim longString As String = <x>line 1
line 2
line 3</x>.Value

Option 4: Array

选项 4:阵列

Dim longString As String = String.Join(Environment.NewLine, {
    "line 1",
    "line 2",
    "line 3"})

Other Options

其他选项

You may also want to consider other alternatives. For instance, if you really want it to be a literal in the code, you could do it in a small c# library of string literals where you could use the @syntax. Or, you may decide that having it in a literal isn't really necessary and storing the text as a string resource would be acceptable. Or, you could also choose to store the string in an external data file and load it at run-time.

您可能还需要考虑其他替代方案。例如,如果您真的希望它成为代码中的文字,您可以在一个小型的 c# 字符串文字库中使用该@语法。或者,您可能决定将它放在文字中并不是真正必要的,并且将文本存储为字符串资源是可以接受的。或者,您也可以选择将字符串存储在外部数据文件中并在运行时加载它。

回答by sloth

You can also simply use a text resource (Project -> Properties -> Resources) and access them in your code via My.Resources.NameOfTheResource.

您也可以简单地使用文本资源(项目 -> 属性 -> 资源)并通过My.Resources.NameOfTheResource.

回答by MansoorShaikh

In VB.net you can simply write "& _" at the end of your string literal to have multi-line strings.

在 VB.net 中,您可以简单地在字符串字面量的末尾写入“& _”以获得多行字符串。