Android Question Parsing string.

Pepebis

Member
Licensed User
Longtime User
Dear All,
I'm receiving two strings form RPi .
One is with Temperature second with humidity.
Strings are like this for example: T30.3 and for humidity H22.1 .
I don't know how I supposed divide those strings and send them on gauges.
Any idea?
Best Regards,
Semek
 

stevel05

Expert
Licensed User
Longtime User
If the identifying character always has a length of 1 then you can use Substring : Dim Val As Double = Str.Substring(1) Casting is taken care of automatically
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Or to be a little clearer:
B4X:
Dim Str As String = "T3.1"
    Dim Temp, Humid As Double
    If Str.StartsWith("T") Then Temp = Str.SubString(1)
    If Str.StartsWith("H") Then Humid = Str.SubString(1)

Or

B4X:
Dim Str As String = "T3.1"
    Dim Temp, Humid As Double
    If Str.CharAt(0) = "T" Then Temp = Str.SubString(1)
    If Str.CharAt(0) = "H" Then Humid = Str.SubString(1)
 
Upvote 0

Pepebis

Member
Licensed User
Longtime User
Hi . It depends on me in what form I will send strings from rpi .

Sub udpsrv_PacketArrived (Packet As UDPPacket)

Dim msg As String
msg = BytesToString(Packet.Data,Packet.Offset,Packet.Length, "UTF8" )

End Sub .

I'm receiving packages but can't divide them.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ok, that is a slightly different question.

How is the string you obtain from the packet formatted?
 
Upvote 0

Pepebis

Member
Licensed User
Longtime User
I created two gauges using some of Earls libs.
Data which im sending from Rpi is just string( numer in format ( for example 33.33) . But that is form one channel . Everything is working fine. But i want to format them cos want to send data from two channels and duno how to do this.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Will there only ever be 2 channels? or will you add more?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ok if you are sending the string then you don't need to add an identifier as you know the order of the data. If you Format the string as "TempVal,HumidVal" you can do:
B4X:
    Dim str As String = "30.3,22.1"                            'Received string
    
    Dim Pos As Int = str.IndexOf(",")
    
    Dim Temp As Double = str.SubString2(0,Pos)
    Dim Humid As Double = str.SubString(Pos + 1)
    
    Log(Temp)
    Log(Humid)
 
Upvote 0

Pepebis

Member
Licensed User
Longtime User
What I'm doing is I'm continuously ( every second ) sending strings to the app .
one is tem another one is hum .
The format is : for temp T22.3 for hum H33.4 ( for example ) I'm using this prefix to
distinguish them .
Just want to show value ( in this case 22.3 on one gauge and 33.4 on another ) .
Best Regards,
Semek
 
Upvote 0

Pepebis

Member
Licensed User
Longtime User
Hi. I don't know. I have tried to crate as simple as possible test app .
From your example but it is showing empty places in editText .



Public Sub test

Dim str As String = "30.3,22.1" 'Received string

Dim Pos As Int = str.IndexOf(",")

Dim Temp As Double = str.SubString2(0,Pos)
Dim Humid As Double = str.SubString(Pos + 1)

Log(Temp)
Log(Humid)

EditText1.Text = EditText1.Text&str
EditText2.Text = EditText2.Text&Temp
EditText3.Text = EditText3.Text&Humid

End Sub


Best Regards
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
Dim str As String = "30.3,22.1" 'Received string
    Dim strArray() As String =Regex.Split(",",str)
    Dim Temp As Double = strArray(0)
    Dim Humid As Double = strArray(1)
    Log(Temp)  'displays 30.3
    Log(Humid) 'displays 22.1
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Dim str As String = "30.3,22.1" 'Received string Dim strArray() As String =Regex.Split(",",str) Dim Temp As Double = strArray(0) Dim Humid As Double = strArray(1) Log(Temp) 'displays 30.3 Log(Humid) 'displays 22.1
That would have been my suggestion if there had been a lot of parameters, I think Regex.Split is quite heavy and fairly slow compared to one or two string splits.

From your example but it is showing empty places in editText .
It is going to depend on what is in the EditText's before you append the data. Presumably it's logging the values OK.
 
Upvote 0

Pepebis

Member
Licensed User
Longtime User
Dear All, previous example posted by Steve :

Dim str As String = "30.3,22.1" 'Received string

Dim Pos As Int = str.IndexOf(",")

Dim Temp As Double = str.SubString2(0,Pos)
Dim Humid As Double = str.SubString(Pos + 1)

Log(Temp)
Log(Humid)

Working perfectly. Thank you very much.
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
HI
And if you want to send more than two characters, regex is best:
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)
 
Upvote 0
Top