如何在 VB.NET 中检测字符串中的特定字符?

发布于 2024-11-19 16:33:58 字数 258 浏览 4 评论 0原文

好的,所以在我正在 VB.NET 中编写的程序中,我尝试制作它,以便可以接收字符串列表(每个字符串位于不同的行)。对于每一行,我想将其分成三部分。第一部分从字符串的开头到字符串中的第一个冒号,第二部分从第一个冒号到 at 符号,最后一部分从 at 符号到字符串的末尾。

例如,我会采用一系列行中的一行: hello:world@yay

我想将其分成三个单独的字符串“hello”、“world”和“yay”。

我该如何在 VB.NET 中做这样的事情呢?

Okay, so in a program I'm working on in VB.NET I'm trying to make it so I can take in a list of strings (each on a different line). For each line I want to take in the line, and break it up into three parts. The first part goes from the beginning of the string to the first colon in the string, the second part goes from the first colon to the at symbol, and the last part goes from the at symbol to the end of the string.

For example, I'd take in a line of the series of lines:
hello:world@yay

I'd want to break it into three separate strings of "hello", "world", and "yay".

How would I do such a thing in VB.NET?

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

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

发布评论

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

评论(3

白馒头 2024-11-26 16:33:59

您可以通过 Split 来完成此操作。出于示例目的,我正在重新分割一个本来可以保存的字符串,因此我不必再次Split它。但是,这样理解更简单:

Dim s as String = "hello:world@yay" 'This can be a string from a loop.
Dim hello As String = s.Split(":")(0)  'Get everything before colon.
Dim world As String = s.Split(":")(1).Split("@")(0) 'Get everything after colon, and split the result again, grabbing everything before the amp.
Dim yay As String = s.Split(":")(1).Split("@")(1) 'Get everything after colon, and split the result again, grabbing everything after the amp.

如果您正在读取文本文件,例如

    Dim objReader As New StreamReader("c:\test.txt")
    Dim s As String = ""
    Dim hello As String
    Dim world As String
    Dim yay As String

    Do
        s = objReader.ReadLine()
        If Not s Is Nothing Then
           hello = s.Split(":")(0)
           world = s.Split(":")(1).Split("@")(0)
           yay = s.Split(":")(1).Split("@")(1)
        End If
    Loop Until s Is Nothing
    objReader.Close()

You can accomplish this with a Split. For example purposes, I am re-splitting a string which I could have saved off, so I wouldn't have to Split it again. However, it's simpler to understand this way:

Dim s as String = "hello:world@yay" 'This can be a string from a loop.
Dim hello As String = s.Split(":")(0)  'Get everything before colon.
Dim world As String = s.Split(":")(1).Split("@")(0) 'Get everything after colon, and split the result again, grabbing everything before the amp.
Dim yay As String = s.Split(":")(1).Split("@")(1) 'Get everything after colon, and split the result again, grabbing everything after the amp.

If you're reading from a text file, e.g.

    Dim objReader As New StreamReader("c:\test.txt")
    Dim s As String = ""
    Dim hello As String
    Dim world As String
    Dim yay As String

    Do
        s = objReader.ReadLine()
        If Not s Is Nothing Then
           hello = s.Split(":")(0)
           world = s.Split(":")(1).Split("@")(0)
           yay = s.Split(":")(1).Split("@")(1)
        End If
    Loop Until s Is Nothing
    objReader.Close()
百思不得你姐 2024-11-26 16:33:59

使用 split 命令

首先使用“:”,然后用“@”分割第二个编辑元素

Use the split command

Start by splitting the string with the ":" and then split the second edit element with the "@"

北风几吹夏 2024-11-26 16:33:59

查看 string.indexOf 并从那里获取它

Look at string.indexOf and take it from there

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