I keep making this sort of mistake: ```s.replace("...
# random
e
I keep making this sort of mistake:
Copy code
s.replace("\\s+", "")
d
Took me a while to parse that. Didn't think anyone actually relied on that feature.
e
I call
String.replace()
with a regular expression a lot, although usually I use a pre-compiled pattern for efficiency. Do you see the bug in my code?
s
no
.toRegex()
or
Regex()
call means you’re using
.replace(String, String)
instead of
.replace(Regex, String)
, the former of which iirc is just a simple search and replace
🎯 1
e
Yeah, I meant
s.replace(Regex("\\s+"), "")
l
An alternative would be
s.replace("\\s+".toRegex(), "")
Maybe this should be a warning from IntelliJ
👍 1
If it could detect that you're trying to use the string as a regex
e
@LeoColman Can you write a regex to detect that? 😉