【问题标题】:String is too long to fit on one line - VB.NET字符串太长,无法放在一行中 - VB.NET
【发布时间】:2014-07-09 14:23:31
【问题描述】:

我有一个长度为 109000 个字符的字符串,但它不适合一行,我得到“行太长”。错误。
我知道使用普通代码,您可以在代码末尾添加“_”,例如

    this code is too long so I will use the _
    this code acts like it is on the same line

但在字符串中,它需要“_”作为字符串的一部分(应该如此)。我找不到这方面的信息,所以这里是给你们的,stackoverflow。

【问题讨论】:

    标签: vb.net string visual-studio


    【解决方案1】:
    Dim longString As String = "loooooooooooooooooooooooooo" + _
    "ooooooooooooooggggggg"
    

    【讨论】:

    • 我怎么没想到
    • @Fabio vbNewLine 我的意思是在运行时,而不是在编码级别。
    • 好的,但据我所知,问题是关于在代码中编写字符串,这就是我问的原因。
    • 对不起,如果我没有指定它
    【解决方案2】:

    documentation on the "Line is too long" error 声明最大行长度为 65535,所以这就是您收到错误的原因。

    有几个解决方案:

    您可以使用& 连接字符串

    Dim s As String = "this code is too long so I will use the" &
                      "this code acts like it is on the same line"
    

    请注意,您也可以使用+ 连接字符串,但如果这样做,请确保您有 Option Strict On(& 更安全,因为结果始终是字符串)。在此处查看两者之间的比较:Ampersand vs plus for concatenating strings in VB.NET

    您可以使用字符串生成器。如果您不断向原始字符串添加字符串(尤其是在循环中执行此操作),这可能会更有效:

    Dim sb As new StringBuilder
    sb.Append("this code is too long so I will use the")
    sb.Append("this code acts like it is on the same line")
    
    Debug.Writeline(sb.ToString)
    

    请参阅 MSDN 以获取 More information about Concatenation here

    【讨论】:

    • 对于顶部代码,您不应该在“&”之后使用“_”来表示您正在移动到下一行吗? @马特威尔科
    • @user3659330 - 否。请参阅 VB coding conventions 声明“避免使用显式行继续符“_”以支持语言允许的隐式行继续。”
    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多