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
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
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)
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.
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)
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
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
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.
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)