开发者

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

I'm trying to convert this code from C# to VB.NET

string[] lines = the开发者_如何学JAVAText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

Here's what I have, the problem is it is printing the whole of the text box contents in the messagebox, instead of each line.

    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


You can't use backslash (\) to escape characters in VB. Use the ControlChars class:

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


Escape sequences don't really exist in VB .Net as far as string literals are concerned.

There are 2 special constants which you can use instead:

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

Should do the trick (untested though).


From an online converter:

Your c# code:

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

Converted to VB.NET:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜