is there a fancy way to replace multiple consecuti...
# announcements
l
is there a fancy way to replace multiple consecutive whitespaces with single whitespaces ? or do i just repeatedly do
.replace("  ", " ")
until the string doesnt change?
c
Use Regex:
.replace("\\s+".toRegex(), "")
l
oh cool
c
Wouldn't that remove all the spaces? I believe the 2nd parameter should be
" "
, not
""
c
@Chilli yes, you’re right, my bad.
.replace("\\s+".toRegex(), " ")
c
Wouldn't regex be an overkill, though? I'm not sure how fast regex actually is but it seems there might be something faster
j
No, this is the kind of problem regular expressions were designed to solve. Any regex is transformable to a finite state machine, so it just depends on the efficiency of the compiler. The only alternative is hand coding it, which is indeed overkill.
👆 1
k
This is a very easy regex, but I would store the pattern though.
d
Regex is built for this and is so fast. Another solution is to build your own parser/ parser combinator. But Regex is the perfect solution for a one off.