Java String replace()
Replacing a single character
Java String replace() method replaces all existing occurrences of a character in a String with another character.
Replacing char sequences
Java String replaceAll() method replaces all the substrings with the replacement String.
Replacing first occurrence
Java String replaceFirst() method replaces the first substring that fits the specified with the replacement String.
The replaceAll() method allowed Regular Expressions to replace all occurrences of matching char sequences inside a String.
Difference between String replace() and replaceAll()
Java String replace method either takes a pair of char’s or a pair of CharSequence . The replace method will replace all occurrences of a char or CharSequence. On the other hand, both String arguments to replaceFirst and replaceAll are regular expressions (regex). In the case of performance, the replace() method is a bit faster than replaceAll() because the replaceAll() first compiles the regex pattern and then matches before finally replacing whereas the replace() simply matches for the provided argument and replaces.