Java String replace(), replaceFirst() and replaceAll() Method

Java String replace(), replaceFirst() & replaceAll() Method EXAMPLE

Java String has three types of Replace method

1. Java String replace() Method


This Java method returns a new string resulting from replacing every occurrence of characters with a new characters



Original String is ‘: the quick fox jumped
String after replacing ‘fox’ with ‘dog’: the quick dog jumped
String after replacing all ‘t’ with ‘a’: ahe quick fox jumped

2.Java String Replaceall()


The java string replaceAll() method returns a string replacing all the sequence of characters matching regular expression and replacement string.


regx: regular expression

replacement: replacement sequence of characters

Example 2


rgex − the regular expression to which given string need to matched.

replacement − the string that replaces regular expression.

Return Value

This method returns resulting String as an output.

Example 3: