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

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

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

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

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

从在线转换器 :

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