簡體   English   中英

VB.NET拆分新行(C#轉換)

[英]VB.NET split on new lines (C# conversion)

我正在嘗試將此代碼從C#轉換為VB.NET

string[] lines = theText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

這就是我所擁有的,問題是它是在消息框中打印整個文本框內容,而不是每行。

    Dim Excluded() As String

    Dim arg() As String = {"\r\n", "\n"}

    Excluded = txtExclude.Text.Split(arg, StringSplitOptions.None)

    For i As Integer = 0 To Excluded.GetUpperBound(0)
        MessageBox.Show("'" & Excluded(i) & "'")
    Next

您不能使用反斜杠( \\ )來轉義VB中的字符。 使用ControlChars類:

Dim arg() As String = { ControlChars.CrLf, ControlChars.Lf }

就字符串文字而言,轉義序列在VB .Net中並不存在。

您可以使用2個特殊常量:

vbCrLf
vbLf

Dim Excluded() As String

Dim arg() As String = {vbCrLf, vbLf}

Excluded = txtExclude.Text.Split(arg, StringSplitOptions.None)

For i As Integer = 0 To Excluded.GetUpperBound(0)
    MessageBox.Show("'" & Excluded(i) & "'")
Next

應該做的伎倆(雖然未經測試)。

來自在線轉換器

你的c#代碼:

string[] lines = theText.Split(new string[] { "\\r\\n", "\\n" }, StringSplitOptions.None);

轉換為VB.NET:

Dim lines As String() = theText.Split(New String() {vbCr & vbLf, vbLf}, StringSplitOptions.None)

暫無
暫無

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

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