VB.NET拆分新行(C#转换)

时间:2011-06-20 07:43:37

标签: c# vb.net string split

我正在尝试将此代码从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

3 个答案:

答案 0 :(得分:9)

您不能使用反斜杠(\)来转义VB中的字符。使用ControlChars类:

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

答案 1 :(得分:8)

就字符串文字而言,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

应该做的伎俩(虽然未经测试)。

答案 2 :(得分:2)

来自online converter

您的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)