Android Question Parsing few strings

Pepebis

Member
Licensed User
Dear All , thank you for the previous response.
Although I'm having another problem here.
As I'm sending strings from sensors in format ( example: 22.4, 33.6 ) and parsing that to each value and next sending it into gauges and that is working just fine.
I'm also using a slider to set proper values on my external device ( this is also working ).
The problem is when I want to send another string to confirm value of slider on the external device and on the app.
I can't put all of this data in one string ( form device-side because I'm only some time changing this value, from another side reading data from sensors is being done every second ). So my idea was to create another string something like this: VAL.33 ( position of the slider on an external device ) which I will be sending just when I have change value.
And this is creating a problem with the previous parsing. So to sum up. I want to send two different strings and make two different parsings at the same time.
Best Regards ,
Semek
 

walterf25

Expert
Licensed User
@walterf25,
B4X:
ReceivedString (0) to „VAL.33”
This is a mistake. Regex doesn't index from zero, it indexes from 1
B4X:
ReceivedString (1)
@Pepebis
put the code in the appropriate tags

Send it all in one go. All you have to do is form it properly. Create the right values between the separators. The separator can be any character (almost any) in this example is "*".
B4X:
    Dim str As String = "*31.1*32.2*33.3*34.4*"
    Dim str1,str2,str3,str4 As String
    Dim numbers() As String
    numbers = Regex.Split("\*", str)
    str1=numbers(1)
    str2=numbers(2)
    str3=numbers(3)
    str4=numbers(4)
If you don't want to change the settings every second, send an empty character or prefix. Now you can get the desired action using conditional statements or select case etc.
You are very wrong my friend, the array that will be returned with Regex.Split is 0 based, try it you'll see.
Walter
 
Upvote 0

kisoft

Well-Known Member
Licensed User
You are very wrong my friend, the array that will be returned with Regex.Split is 0 based, try it you'll see.
Walter

I checked
B4X:
Dim str As String = "*31.1*32.2*33.3*34.4*"
    Dim str1,str2,str3,str4 As String
    Dim numbers() As String
    numbers = Regex.Split("\*", str)
    str1=numbers(0)
    Log("str1="&str1)
    str2=numbers(1)
    Log("str2="&str2)
    str3=numbers(2)
    Log("str3="&str3)
    str4=numbers(3)
    Log("str4="&str4)

and here's the score:

B4X:
str1=
str2=31.1
str3=32.2
str4=33.3
 
Upvote 0

walterf25

Expert
Licensed User
I checked
B4X:
Dim str As String = "*31.1*32.2*33.3*34.4*"
    Dim str1,str2,str3,str4 As String
    Dim numbers() As String
    numbers = Regex.Split("\*", str)
    str1=numbers(0)
    Log("str1="&str1)
    str2=numbers(1)
    Log("str2="&str2)
    str3=numbers(2)
    Log("str3="&str3)
    str4=numbers(3)
    Log("str4="&str4)

and here's the score:

B4X:
str1=
str2=31.1
str3=32.2
str4=33.3
LOL, that is because your String is this

B4X:
Dim str As String = "*31.1*32.2*33.3*34.4*"
So yes of course the first element in the returned array will be nothing because you are splitting the string at "*", instead your string should look like this:
B4X:
Dim str As String = "31.1*32.2*33.3*34.4"
Remove the leading "*" and the ending "*"

Walter
 
Upvote 0

kisoft

Well-Known Member
Licensed User
@walterf25 Ok I checked, you're right.
But when transferring data, it is better to use the first method and create everything between separators.
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

emexes

Expert
Licensed User
But when transferring data, it is better to use the first method and create everything between separators.
If you are calling them separators, then they go in-between fields. If you have n fields, there should be n-1 separators. Just like with CSV format, where n fields means n-1 commas.

If you do it this way, then the regex .Split function will work precisely the way you expect and need it to work. Only ambiguity is an empty string. An empty string could be interpreted as either: no field, or: one empty field. But you seem to already be filtering out empty strings, so... no problem :)
 
Last edited:
Upvote 0

Pepebis

Member
Licensed User
Gents thank you for all of your emails.

Screen Shot 12-30-19 at 10.03 AM 001.PNG

But how i can get values if I'm sending strigs like this ( 3 first rows ) ?
There is no problem with parsing string ( last one ) if all data is coming in one string.
Best Regards
 
Upvote 0

sorex

Expert
Licensed User
I don't see what you problem is?

is it this that you want?

B4X:
Dim lines() As String=Array As String("VAL1*33.5","VAL2*38.5","VAL3*44.6","33.5,38.5,44.6")  
For x=0 To lines.Length-1
    parseLine(lines(x))  
Next

Sub parseLine(line As String)
    Dim data() As String
    If line.StartsWith("VAL") Then
        data=Regex.Split("\*",line)
        Log(data(1))   'data(0) will give you the val id
    Else
        data=Regex.Split(",",line)
        For x=0 To data.Length-1
            Log($"multi ${x}:${data(x)}"$)
        Next
    End If  
End Sub

Waiting for debugger to connect...
Program started.
33.5
38.5
44.6
multi 0:33.5
multi 1:38.5
multi 2:44.6
 
Upvote 0

Pepebis

Member
Licensed User
Let me check . My idea was somethig like this :

B4X:
ub udpsrv_PacketArrived (Packet As UDPPacket)
    
    Dim str As String                            'Received string
    str = BytesToString(Packet.Data,Packet.Offset,Packet.Length, "UTF8" )
    
    
    Dim Temp As Double
    Dim Humid As Double
    
    
    
    
    
    
    If str.StartsWith("*TEMP:*")Then
        Log(Temp)
        Gauge1.CurrentValue = Temp
        
    End If
    If str.StartsWith("*HUM:*")Then
        Log(Humid)
        Gauge2.CurrentValue = Humid
        
    End If
 
Upvote 0

Pepebis

Member
Licensed User
Dear All, Thank you for your time and tremendous help. Special thanks to Mr.Kisoft . I'm attaching code that I was able to create with your help and has been tested end working just fine.
Best Regards to All,
Semek
B4X:
ub udpsrv_PacketArrived (Packet As UDPPacket)
   
   
    Dim str As String
    str = BytesToString(Packet.Data,Packet.Offset,Packet.Length, "UTF8" )
    Dim str1, str2 As String
    Dim numbers() As String
    numbers = Regex.Split("\^", str)
    str1=numbers(0)
    str2=numbers(1)
   
   
    Select str1
        Case "Temp1"
            Log(str2)
            Gauge1.CurrentValue = str2
            'Tutaj teraz możesz wstawić dowolny kod żeby obsłużyć aplikację
        Case "Hum1"
            Log(str2)
        Gauge2.CurrentValue = str2
            'Tutaj teraz możesz wstawić dowolny kod żeby obsłużyć aplikację
        Case "Sla1"
            Log(str2)
            'Tutaj teraz możesz wstawić dowolny kod żeby obsłużyć aplikację
       SeekBar1.Value= str2
       Label2.Text=str2
        '   RoundSlider1_ValueChanged(str2)
    End Select
   
   
End Sub
 
Last edited:
Upvote 0
Top