My URL in Kotlin has an extra & at the end of it

You can simply do it in a functional way:

val parameters = { (key, value) -> // 1
    "$key=$value"                 // 2
}.joinToString("&")               // 3
return BASE_URL + parameters      // 4


  1. We use map function with destructuring declaration of parameters (key, value) which correspond to key and value in each map entry
  2. For each map entry we create string key=value ($ is indicator, that we want to use value of some variable – in that case we use value of parameter key and parameter value
  3. After .map function we have collection of strings: term=parameter1 and media=parameter2, we are joining them with & separator
  4. Lastly we need to concatenate parameters to base url

If I can suggest – I would make this function more general by putting baseUrl as parameter – then you can use the same function for different urls.


As Михаил Нафталь suggested in his answer, you can add prefix to joinToString function so you don’t have to concatenate parameters with base url. So after that improvement your code will look like this:

return { (key, value) ->
}.joinToString("&", prefix = BASE_URL )

Or you can do all of this using just joinToString function with giving transform function. (look for Михаил Нафталь answer)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top