vb.net中的字符串拆分

时间:2017-03-10 08:28:56

标签: vb.net

我有像这样的传感器数据字符串

str="A34,B32,C60,D54"

我想分割每个传感器的数据,例如A34为我定义为A的第一个传感器,34是变量号,然后我想删除A并将数字放在第一个传感器标签中。

来自串口的传入字符串

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

Dim str As String = "A34,B32,C60,D54"
Dim substrings As String() = str.Split(","c)
For Each substring As String In substrings
   Console.WriteLine(substring.Remove(0, 1))
Next

答案 1 :(得分:0)

试试这段代码:

Dim t() As String
Dim Str = "A34,B32,C60,D54"
t = Split(Str, ",")
For Each s In t       
    Dim m As Match = Regex.Match(s, "^([A-Z]+)([0-9]+)$")
    Dim lt As String = m.Groups(1).Value
    Dim nb As String = m.Groups(2).Value
Next

答案 2 :(得分:0)

以下代码可以解决问题。首先用逗号分隔字符串以获取所有部分,然后构建一个正则表达式以将数字与字母分开。遍历所有部分并匹配正则表达式。在比赛中,您将获得字母和数字部分。

Dim input As String = "A34,B32,C60,D54"
Dim parts As String() = input.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)

Dim regex As Regex = new Regex("([a-zA-Z]+)(\d+)")

For Each part as String in parts

    Dim result as Match = regex.Match(part)
    Dim letter As String = result.Groups(1).Value
    Dim number As String = result.Groups(2).Value

Next

如果字母部分总是1个字符长,你可以使用Kapila Perera的答案。