Hi! How do I replace last space in sting: `one two...
# stdlib
d
Hi! How do I replace last space in sting:
one two three
->
one two\nthree
. There's
replaceFirst
, but no
replaceLast
d
Copy code
val idx = string.lastIndexOf(' ')
val replaced = if (idx < 0) string else string.replaceRange(idx, idx+1, "\n")
Although there should be a
replaceLast
for sure. Not sure why there isn't.
d
Thanks! I have just done the same 🙂
h
Unless your string is huge, the braindead version also works OK: `string.reversed().replaceFirst(" ", "\n").reversed()`…