Hello all๐ I hope you are doing well. This is going to be a very short and very useful article. In this article I'll be writing about replace() method in Java. This is a one of the most useful methods.
Let's begin...
replace()
The replace() method will replace a character or substring with another character or string. This is a method of Java String class. It returns a string derived from the original string by replacing every occurrence of old-string or old-character with new-string or new-character. When working with a string in Java, you may encounter a situation where you want to replace a specific character or substring in that string with another character or another substring. In such situations replace() method comes in.
Syntax :-
stringName.replace(oldString, newString);
There are many variations in which replace() can be used.
1. replace(char oldChar, char newChar)
In this example we have replaced all the occurrences of char โlโ with char โc'.
public class Demo {
public static void main(String[] args) {
String strOld = "Hello world";
System.out.println("Original string : " + strOld);
String strNew = strOld.replace('l','c');
System.out.println("Replaced string : " + strNew);
}
}
You can run your code online here
2. replace(String oldString, String newString)
In this example we have replaced all the occurrence of String "lo" with String "ping".
public class Demo {
public static void main(String[] args) {
String strOld = "Hello world";
System.out.println("Original string : " + strOld);
String strNew = strOld.replace("lo","ping");
System.out.println("Replaced string : " + strNew);
}
}
You can run your code online here
3. replace(String emptyString, String newString)
In this example we have replaced all the occurrence of String "" (empty String) with String "A".
public class Demo {
public static void main(String[] args) {
String strOld = "Hello world";
System.out.println("Original string : " + strOld);
String strNew = strOld.replace("", "A");
System.out.println("Replaced string : " + strNew);
}
}
You can run your code online here
4. replace(String whitespace, String newString)
public class Demo {
public static void main(String[] args) {
String strOld = "Hello world";
System.out.println("Original string : " + strOld);
String strNew = strOld.replace(" ", "WHITESPACE");
System.out.println("Replaced string : " + strNew);
}
}
You can run your code online here
5. replaceFirst(String oldString, String newString)
public class Demo {
public static void main(String[] args) {
String strOld = "Hello world";
System.out.println("Original string : " + strOld);
String strNew = strOld.replaceFirst("l", "c");
System.out.println("Replaced string : " + strNew);
}
}
You can run your code online here
6. replaceAll(String regex, String newString)
public class Demo {
public static void main(String[] args) {
String strOld = "Hello world";
System.out.println("Original string : " + strOld);
String strNew = strOld.replaceFirst("\\s", "");
System.out.println("Replaced string : " + strNew);
}
}
You can run your code online here
Top comments (0)