Java String replace(), replaceAll() and replaceFirst() method
The String Class Java has three types of Replace methods:
- replace()
- replaceAll()
- replaceFirst()
With the help of replace() function in Java, you can replace characters in your string. Lets study each Java string API functions in details:
Java String replace() Method
Java String replace() method replaces every occurrence of a given character with a new character and returns a new string. The Java replace() string method allows the replacement of a sequence of character values.
Syntax:
public Str replace(char oldC, char newC)
Parameters:
oldCh − old character
newCh − new character
Return Value
The Java replace() function returns a string by replacing oldCh with newCh.
Example of replace() in Java:
Let’s understand replace() in Java function with an example:
public class Guru99Ex1 { public static void main(String args[]) { String S1 = new String("the quick fox jumped"); System.out.println("Original String is ': " + S1); System.out.println("String after replacing 'fox' with 'dog': " + S1.replace("fox", "dog")); System.out.println("String after replacing all 't' with 'a': " + S1.replace('t', 'a')); } }
Expected Output:
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
Java String Replaceall()
Java String replaceAll() method finds all occurrences of sequence of characters matching a regular expression and replaces them with the replacement string. At the end of call, a new string is returned by the function replaceAll() in Java.
Signature:
public Str replaceAll(String regex, String replacement)
Parameters:
regx: regular expression
replacement: replacement sequence of characters
Example:
public class Guru99Ex2 { public static void main(String args[]) { String str = "Guru99 is a site providing free tutorials"; //remove white spaces String str2 = str.replaceAll("\\s", ""); System.out.println(str2); } }
Expected Output:
Guru99isasiteprovidingfreetutorials
Java String replaceFirst()
Java String replaceFirst() method replaces ONLY the first substring which matches a given regular expression. Matching of the string starts from the beginning of a string (left to right). At the end of call, a new string is returned by the Java replaceFirst() function.
Syntax
public Str replaceFirst(String rgex, String replacement)
Parameters
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 of replaceFirst() in Java:
public class Guru99Ex2 { public static void main(String args[]) { String str = "This website providing free tutorials"; //Only Replace first 's' with '9' String str1 = str.replaceFirst("s", "9"); System.out.println(str1); } }
Expected Output:
Thi9 website providing free tutorials
How to Replace a Character in a String in JAVA?
Java String replace() method replaces every occurrence of a given character with a new character and returns a new string.
The syntax for the replace() method is string_name. replace(old_string, new_string)