Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String class replaceAll method missing

I'm having a very weird issue right now, the replaceAll method is missing from the String object.

JDK version: 1.8.0
Android Studio version: 3.1.3
Kotlin version: 1.2

enter image description here

It hasn't been removed, so whats going on here??

like image 538
daka Avatar asked Jun 20 '18 10:06

daka


People also ask

What is replaceAll \\ s in Java?

Java String replaceAll() The replaceAll() method replaces each substring that matches the regex of the string with the specified text.

Does Java replaceAll modify original string?

Strings in Java are immutable - when you call replace , it doesn't change the contents of the existing string - it returns a new string with the modifications.

Does replaceAll replace string?

replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.


1 Answers

You can do this with just replace in Kotlin:

"foo and foo".replace("foo", "bar") // "bar and bar" 

Note that the call above replaces the literal strings, if you need a Regex as the first parameter you can do either of the following:

"foo and bar".replace("[abcd]".toRegex(), "x") // "foo xnx xxr" "foo and bar".replace(Regex("[abcd]"), "x")    // "foo xnx xxr" 
like image 154
zsmb13 Avatar answered Sep 30 '22 01:09

zsmb13