0

I have sensors data string like this

str="A34,B32,C60,D54"

I want to split data of every sensor for example A34 for the first sensor that I defined as A and 34 is variable number then I want to remove A and put the number in the first sensor label.

Incoming string from serial port

Can anyone help me?

user31562
  • 97
  • 1
  • 11

3 Answers3

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
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Kapila Perera
  • 837
  • 1
  • 11
  • 24
  • 1
    Nice mixing of C# and VB.NET. – Visual Vincent Mar 10 '17 at 08:39
  • 2
    That `c` you deleted is valid syntax. Have a look at [What does adding a “c” do in a VB.NET character array?](http://stackoverflow.com/questions/19522703/what-does-adding-a-c-do-in-a-vb-net-character-array). Try your code in an IDE with **Option Strict On** and see what happens. – Bugs Mar 10 '17 at 08:57
  • 2
    Indeed it is. `c` is VB.NET's way of declaring a character literal. – Visual Vincent Mar 10 '17 at 08:59
0

Try this code:

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
Bugs
  • 4,491
  • 9
  • 32
  • 41
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
0

Following code does the trick. First split the string by the comma to get all the parts, next build a regular expression to separate the number from the letter. Loop over all the parts and match the regular expression. In the match, you'll get the letter and number part.

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

In case the letter part is always 1 character long, you can use the answer of Kapila Perera.

Abbas
  • 14,186
  • 6
  • 41
  • 72