Am i missing something, or '\s' is not valid "sing...
# getting-started
m
Am i missing something, or '\s' is not valid "single-space" in kotlin?
g
mrtreinis: similar to java, you have to escape the \ with another \, or else use triple quotes. Either of these work:
Copy code
println("a or b".replace("\\s".toRegex(), "_"))
    println("a or b".replace("""\s""".toRegex(), "_"))
m
Notice the single quotes in my example. \n,\t etc seems to work just fine, \s does not
g
oh, ok.. i misunderstood i guess - no you can't do that. not sure why you'd want to when you can just print(' ')
it's also illegal escape in java
m
Yeah, I was hoping. For me ' ' is less readable than '\s'. For example
xxx.trim(' ', '/', '\t', '\n', '\r')
g
i see... you could still use a regex instead, eg
str.replace("[\\t/\\n\\r\\s]".toRegex(), "")
better yet, wrap it up in an extension function:
Copy code
fun String.myTrim() = this.replace("[\\t/\\n\\r\\s]".toRegex(), "")
then call it
xxx.myTrim()