How to split an URL

How can I split URL’s like this one:

file:///E:/Templates/HTML/three_lines.html?f0=24.%20december&f1=Julemanden&f2=Gr%C3%B8nland&startAll=yes

I need the URL and the different parts of the query string separated like:

Key                  Value
Adress               file:///E:/Templates/HTML/three_lines.html
f0                   24.%20december
f1                   Julemanden
f2                   Gr%C3%B8nland
startAll             yes

The number of Key/Value pairs is unknown

well,
you could split between address and variables using the ? character

address = nthfield(URL,"?",1)
variables = nthfield(URL,"?",2)

then cycle on variables to get everyone using “&”

for ind = 1 to countfields(variables,"&")
variableNameAndData = nthfield(variables,"&",ind)
next

then for every variableNameAndData you must split using “=” to separate the variable name from variable value

Giulio, using NthField in a loop like that is slow and should generally be avoided. Instead, use Split.

Here is my version. (Yes, you could also do this with regular expressions.)

  dim address as string = url.Nthfield( "?", 1 )
  dim rest as string = url.Mid( address.Len + 2 ) // Get the rest
  
  dim params as new Dictionary
  dim segments() as string = rest.Split( "&" )
  for each segment as string in segments
    dim param as string = segment.NthField( "=", 1 )
    dim value as string = segment.Mid( param.Len + 2 )
    value = DecodeURLComponent( value )
    params.Value( param ) = value
  next
1 Like

OK, agree that split is better :slight_smile:

Thanks to both of you.
I will try your solution, Kem and see if it works for me – I’m sure it will :slight_smile: