https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jasmin Fajkic

08/15/2022, 4:29 PM
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.
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
j

Jasmin Fajkic

08/15/2022, 5:16 PM
need it in android project
f

Francesc

08/15/2022, 5:57 PM
for Android, you are probably better off converting this to a Uri and then using the parse tools provided by that class. See this https://developer.android.com/reference/android/net/Uri
j

Jasmin Fajkic

08/15/2022, 8:04 PM
thx guys fixed it.
3 Views