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

发布于 2024-11-16 14:16:00 字数 474 浏览 0 评论 0原文

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

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

string[] lines = theText.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趴在窗边数星星i 2024-11-23 14:16:00

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

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

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

Dim arg() As String = { ControlChars.CrLf, ControlChars.Lf }
蹲墙角沉默 2024-11-23 14:16:00

就字符串文字而言,转义序列实际上并不存在于 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

应该可以解决问题(尽管未经测试)。

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

酒几许 2024-11-23 14:16:00

来自 在线转换器

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文