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

前端 未结 3 1045
别跟我提以往
别跟我提以往 2021-01-05 02:30

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

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


        
相关标签:
3条回答
  • 2021-01-05 03:10

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

    Dim arg() As String = { ControlChars.CrLf, ControlChars.Lf }
    
    0 讨论(0)
  • 2021-01-05 03:15

    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 讨论(0)
  • 2021-01-05 03:16

    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).

    0 讨论(0)
提交回复
热议问题