Android Question Conversion from String to Integer...impossible????

Stephaner

Member
Licensed User
Longtime User
Hello,

I'm a new user of B4A and I have to convert small parts of strings into Integers. But it looks impossible???? I tryed several issues (split, matcher,...) but without result. I think split is a good idea to resolve my problem but the Pattern is too difficult to understand !!!!



Here are exemples of the strings I receive :
"1000 addr[75] protocol[DCC28] name[CC 72000]"
"1001 addr[752] protocol[DCC128] name[141R patrick]"
"1025 addr[4] protocol[DCC28] name[Y8000]"
...

I need to :
- catch the first value (1000, 1001, 1025, etc...) in an array of Integers : ID()
- catch the adress number (75, 752, 4,...) in an Array of Integers : Adress()
- catch the third number (28,128,28...) in an array of Integers : Protocole()
- catch the names ("CC72000", "141R Patrick", 'Y8000",...) in an array of Strings : Name()

The last one is not too difficult but for the others I can't believe that there is not possibility to translate into Integer....
Can anyone help me?
Thanks,
Stéphane.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Devlei is also correct but when the strings or numbers are different lengths then it would be easier with regex I think

Example using :"1025 addr[4] protocol[DCC28] name[Y8000]"

B4X:
m = Regex.Matcher( "\d+ ", teststring)
if m.find then
firstnumber=m.Match.SubString2(0,m.Match.Length)

should catch 1025 with a space at then end. Take value of string to get 1025.

To catch "[4]", try the catch string "\[\d+\]" gives "[4]"
to catch "[DCC28]" try "\[DCC\d+\]" gives "[DCC28]"
to catch "[Y8000" try "name\[.+\]" gives "name[Y8000]"

Hope this helps.
Mark
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
the below code only will work if your incoming strings are of a standard format as the ones you supplied above..
You could either process them as you receive them , or place them in a list for bulk processing later...
It might also be better to use Lists rater than Arrays for extracted data ..

Regex might still be the best way but ...

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim lstIncomming As List
    Dim arrID(3) , arrAddress(3), arrProtocol(3) as Int
    Dim arrName(3) As String

End Sub

Sub Activity_Create(FirstTime As Boolean)

    lstIncomming.Initialize
    lstIncomming.Add("1000 addr[75] protocol[DCC28] name[CC 72000]")
    lstIncomming.Add("1001 addr[752] protocol[DCC128] name[141R patrick]")
    lstIncomming.Add("1025 addr[4] protocol[DCC28] name[Y8000]")

    For  i = 0 To lstIncomming.Size - 1
        getID(lstIncomming.get (i), i)
        getAddress(lstIncomming.get(i) ,i)
        getProtocol(lstIncomming.get(i), i)
        getName(lstIncomming.get(i), i)
    Next

     For  i = 0 To 2
       Log  ("ID  " & arrID(i))
       Log  ("Address  " & arrAddress(i))
       Log  ("Protocol  " & arrProtocol(i))
       Log  ("Name  " & arrName(i))
     Next

End Sub

Sub getID(strRec As String , iRec As Int)
    Dim  iStart, iStop As Int
    iStart=0
    iStop=strRec.IndexOf("addr[") -1

    arrID(iRec) = strRec.SubString2(iStart,iStop)
End Sub

Sub getAddress(strRec As String , iRec As Int)
    Dim  iStart, iStop As Int
    iStart=strRec.IndexOf("addr[") +5
    iStop=strRec.IndexOf("] protocol")

    arrAddress(iRec) = strRec.SubString2(iStart,iStop)
End Sub

Sub getProtocol(strRec As String , iRec As Int)
    Dim  iStart, iStop As Int
    iStart=strRec.IndexOf("protocol[") + 9
    iStop=strRec.IndexOf("] name")

    arrProtocol(iRec) = strRec.SubString2(iStart,iStop)
End Sub

Sub getName(strRec As String , iRec As Int)
    Dim  iStart, iStop As Int
    iStart=strRec.IndexOf("name[") +5
    iStop=strRec.Length - 1

    arrName(iRec) = strRec.SubString2(iStart,iStop)
End Sub

Cheers mj ... EDIT ... Forgot to mention ... Nothing is Impossible .
 
Last edited:
Upvote 0

Stephaner

Member
Licensed User
Longtime User
Woaoh !!!
Incredible...
I'm impressed.

When I see these solutions I still can't understand why my code didn't translate the strings into Int...
May be because I had forgotten to give a length to my array? I had declared :
B4X:
Dim ID() As Int
...But I think it is not correct.

When I have given a special length to my array :...
B4X:
ID(120) As Int
...it worked better but I used a very long method whith a lot of tests. More over, it gives a maximum fixed value for the array. That was not good.


So, I'm going to insert your method in my program. It will be certainly shorter than the one I have writen.

THANKS to all.
 
Upvote 0

beni_feld

Member
Licensed User
Longtime User
An easy way to solve the problem
B4X:
Sub SplitRecord
Dim DataIn As String
Dim Id As Int
Dim addr As String
Dim protocol As String
Dim name As String
 
 
DataIn="1000 addr[75] protocol[DCC28] name[CC 72000]"
DataIn=DataIn.Replace(" addr",",")
DataIn=DataIn.Replace(" protocol",",")
DataIn=DataIn.Replace(" name",",")
DataIn=DataIn.Replace("[","")
DataIn=DataIn.Replace("]","")
'"1000,75,DCC28,CC 72000"  Converted into array
 
 
Dim Data() As String
Data=Regex.Split(",",DataIn)
Id=Data(0)
addr=Data(1)
protocol=Data(2)
name=Data(3)
Log (Id & " " & addr & " " & protocol & " " & name)
End Sub

BN
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
@ beni_feld ... much nicer

@ Stephaner ... store your extracted data in lists

Cheers mj
 
Upvote 0
Top