How to decode URL to a map object

EduardoElias

Active Member
Licensed User
Longtime User
Hi there!

I have searched over the forum and could not find an answer.

I am using the same sintax of param for a URL to comunicate over UDP.

like this:

ID=<text>&CMD=<number>& ....

This is the same used on URL for params.

But this is not a URL, just a String that I send and receive.

I know I could parse it, but i dont want to do another procedure again for that,

I wonder if there is something ready that parse this string giving me value for each key. Or transform this string in a map to retrive the values for each key.

Thanks in advance!

Eduardo Elias
 

EduardoElias

Active Member
Licensed User
Longtime User
Well, I just made something for that, did not know about regex before that. However if there is something standard already implemented please let me know

B4X:
Sub DecodeParams(Msg As String) As Map
   Dim stru As StringUtils
   Dim parts() As String = Regex.Split("&", Msg)
   Dim Params As Map
   Params.Initialize
   
   For i = 0 To parts.Length - 1
      Dim pair() As String = Regex.Split("=", parts(i))
      If pair.Length >= 2 Then
         Params.Put(pair(0), pair(1))
      End If
      If pair.Length = 1 Then
         Params.Put(pair(0), "")
      End If
   Next

   Return Params
End Sub
 
Upvote 0
Top