Android Question String to Map

Discussion in 'Android Questions' started by AHilberink, May 9, 2019.

  1. AHilberink

    AHilberink Active Member Licensed User

    Hi,

    For my project I needed a conversion from string to map.
    Code:
    'Sep=Separator between Key and Value
    'Tkn=Token between the Map values
    'Returns Map
    Sub String2Map(Str As String,Sep As String,Tkn As StringAs 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)=TrueThen
            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.
     
    José J. Aguilar likes this.
  2. Erel

    Erel Administrator Staff Member Licensed 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?
     
  3. AHilberink

    AHilberink Active Member Licensed User

    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é
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice