The main thing I’m struggling to check is spaces between characters. I wanted to change a
" " into "_" but only when there is another letter after it… I can’t manage to do it. my current check is:
for (c in userName) {
if ("$c" == " ") { //How do I check in here if after c comes another word??
userName = userName.replace("$c", "_")
}
}
2
Answers
What about something like this:
This is easier using the APIs provided in Kotlin
I left some comments to clarify each individual step.
Generally, manual manipulation of strings will be more difficult than the provided APIs
Realistically, usage of RegEx would be your best friend, but I wanted to keep this code related rather than solving it in that manner.
Also, in Kotlin, there is a distinct difference between double quotes
"
and single quotes'
Double quotes are a string, and Single quotes are a Char. There is a different API under the 2.
I use the single quote in string iterator methods to keep it more simple and efficient, rather than having to create new strings each time