Android Question String to Map

AHilberink

Active Member
Licensed User
Longtime User
Hi,

For my project I needed a conversion from string to map.
B4X:
'Sep=Separator between Key and Value
'Tkn=Token between the Map values
'Returns Map
Sub String2Map(Str As String,Sep As String,Tkn As String) As Map
    Dim StrMap As Map
    
    StrMap.Initialize
    StrMap.Clear
    Do While Str.Contains(Tkn)
        StrMap.Put(Str.SubString2(0,Str.IndexOf(Sep)),Str.SubString2(Str.IndexOf(Sep)+1,Str.IndexOf(Tkn)))
        Str=Str.SubString2(Str.IndexOf(Tkn)+1,Str.Length)
    Loop
    If(Str.Contains(Sep)=True) Then
        StrMap.Put(Str.SubString2(0,Str.IndexOf(Sep)),Str.SubString2(Str.IndexOf(Sep)+1,Str.Length))
    End If
    
    Return StrMap   
End Sub

May be usefull for others.
 

AHilberink

Active Member
Licensed User
Longtime User
There is an assumption here that the separator and token between the values it not part of the keys or values.

Why not use JSON? Or File.ReadMap / WriteMap? Or B4XSerializator?

I had to break a long parameterstring, like a=1&b=2&c=3 etc., into a Map because of HTTP.PostMultipart. With this function I got back the Map needed.
I could not find a straight function for it, but maybe there was/is.

Best regards,
André
 
Upvote 0
Top