Home > Enterprise >  can any one explain difference between replace, replaceAll and replaceAllLiterally in scala?
can any one explain difference between replace, replaceAll and replaceAllLiterally in scala?

Time:11-25

what is the difference between replace, replaceAll and replaceAllLiterally in Scala or Java? I have searched in web but i not clear about these things.so can anyone explain

CodePudding user response:

replace and replaceAllLiterally are similar, and in fact the latter is deprecated. From docs:

(Since version 2.13.2) Use s.replace as an exact replacement

replaceAll on the other hand matches the string with regex and replaces it with the replacement string.

CodePudding user response:

In java, the replace method either takes a pair of char's or a pair of CharSequence's (of which String is a subclass, so it'll happily take a pair of String's). The replace method will replace all occurrences of a char or CharSequence. On the other hand, the first String arguments of replaceFirst and replaceAll are regular expressions (regex). Using the wrong function can lead to subtle bugs.

For more information refer the documentation

  • Related