簡體   English   中英

字符串太長而無法放在一行上-VB.NET

[英]String is too long to fit on one line - VB.NET

我有一個長度為109000個字符的字符串,它不能放在一行上,但顯示“行太長”。 錯誤。
我知道,使用普通的CODE,您可以在代碼末尾添加“ _”,例如

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

但是在字符串中,它必須將“ _”作為字符串的一部分(應如此)。 我沒有關於此的任何信息,所以這里是適合您的stackoverflow。

關於“行過長”錯誤文檔指出最大行長為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( &更安全,因為結果始終是String)。 在此處查看兩者之間的比較:& 符與加號,用於在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。

Dim longString As String = "loooooooooooooooooooooooooo" + _
"ooooooooooooooggggggg"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM