here are three variants of replace() method.This article depicts about all of them ,as follows:
1. String replace():This method returns a new string resulting from replacing all occurrences of old characters in the string with new characters.
Syntax: public String replace(char oldch, char newch) Parameters: oldch : the old character. newch : the new character. Return Value It returns a string derived from this string by replacing every occurrence of oldch with newch.
After replacing all o with T : WelcTme tT geeksfTrgeeks After replacing all e with D : WDlcomD to gDDksforgDDks
2. String replaceAll() :This method replaces each substring of the string that matches the given regular expression with the given replace_str.
Syntax: public String replaceAll(String regex, String replace_str) Parameters: regex: the regular expression to which this string is to be matched. replace_str:the string which would replace found expression. Return Value This method returns the resulting String.
Original String : Welcome to geeksforgeeks After replacing regex with replace_str : ASTHA TYAGI
3. String replaceFirst() :This method replaces the first substring of this string that matches the given regular expression with the given replace_str.
Syntax public String replaceFirst(String regex, String replace_str) Parameters regex :the regular expression to which this string is to be matched. replace_str : the string which would replace found expression. Return Value : This method returns a resulting String.
Original String : Welcome to geeksforgeeks After replacing 1st occurrence of regex with replace_str : Welcome to ASTHAforgeeks
This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1
- Java.io.InputStream Class in Java
- Java.io.SequenceInputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 2
- Java.io.Console class in Java
- Java.io.FilterOutputStream Class in Java
- Java.io.StringWriter class in Java
- Java.io.CharArrayReader Class in Java
- Java.io.FileInputStream Class in Java
- Java.io.CharArrayWriter class in Java | Set 1
- Java.io.CharArrayWriter class in Java | Set 2
- Java.io.DataInputStream class in Java | Set 1