Hello. I am coming from JS world and want to ask two things:
1. how to replace / from start string? i have path /path/something and want to remove first slash. Tried with
path.replace("/^\\/|\\/\$/g".toRegex(), "")
does not work.
2. what is easiest way to get search params as key value pair. /path/something?boom=true&id=bam. Want to have search params like boom:true , id: bam?
l
Landry Norris
08/15/2022, 4:31 PM
For the first question, you can use
path.trimStart('/')
, which will remove all slashes at the start of the string.
e
ephemient
08/15/2022, 4:34 PM
or
path.removePrefix("/")
to remove just one. the problem with your code is that you are including the
/
delimiters. you would have the same issue if you wrote
RegExp("/^\\/|\\/\$/g")
in JS too.
ephemient
08/15/2022, 4:37 PM
for 2, it depends; what environment are you in? in Kotlin/JS you can use the same
URLSearchParams
class that exists in JS. on Kotlin/JVM there's a few options