Here we will see Java string replace examples with replacing the string, replacing characters, replacing whitespace vowels, and using a regular expression to replace string and substring.
Java string replace Example: use of replace()
Java String replace() is used to replace substring in given string.
1 2 3 4 5 6 7 8 9 | public class JavaString { public static void main(String[] args) { String s1 = " My Name is Joker"; String s2 = s1.replace("Joker", "Poker"); System.out.println("Old String : " + s1); System.out.println("New String : " + s2); } } |
In this example, Joker is replaced with Poker and the updated string is stored in s2.
1 2 | Old String : My Name is Joker New String : My Name is Poker |
Java replace multiple characters in string | replaceall java
In the following example, we replace all occurrences of This to It using relpaceAll().
1 2 3 4 5 6 7 8 9 10 | public class ReplaceAllExample { public static void main(String[] args) { String s1 = "This is a ball. This is a red color ball. This is a heavey ball"; String s2 = s1.replaceAll("This", "It"); System.out.println("Old String :" + s1); System.out.println("New String :" + s2); } } |
Here we are replacing This with It. This comes 3 times using replaceAll()
we can replace multiple occurrences of text.
1 2 | Old String :This is a ball. This is a red color ball. This is a heavey ball New String :It is a ball. It is a red color ball. It is a heavey ball |
String replace java Example
1 2 3 4 5 6 7 8 9 | public class StringReplace { public static void main(String[] args) { String s1 = "Hello how are you"; String s2 = s1.replace("how are you", "I am fine"); System.out.println("Old String : " + s1); System.out.println("New String : " + s2); } } |
1 2 | Old String : Hello how are you New String : Hello I am fine |
java string replace substring
1 2 3 4 5 6 7 8 9 | public class Substring { public static void main(String[] args) { String s1 = "2022 July04 12:08 PM"; String s2 = s1.replace("PM", "AM"); System.out.println("Old String : " + s1); System.out.println("New String : " + s2); } } |
1 2 | Old String : 2022 July04 12:08 PM New String : 2022 July04 12:08 AM |
how to replace a character in a string in java | replace character in string java
Replacing a single character from a string is simple by using replace().
The following example illustrates the same
1 2 3 4 5 6 7 8 9 | public class ReplaceCharacter { public static void main(String[] args) { String s1 = "Alephant"; String s2 = s1.replace('A', 'E'); System.out.println("Old String : " + s1); System.out.println("New String : " + s2); } } |
1 2 | Old String : Alephant New String : Elephant |
java string replace regex
A regular expression is one of the powerful mechanisms in Java. using regular expressions we can reduce our code.
How to remove special characters from a string in java
Java replaceall regex example
1 2 3 4 5 6 7 8 9 | public class StringRegx { public static void main(String[] args) { String s2 = s1.replaceAll("[^a-zA-Z]+", " "); System.out.println("Old String : " + s1); System.out.println("New String : " + s2); } } |
1 2 | Old String : This123is@a99string#example New String : This is a string example |
how to replace a character in a string in java without using replace method
This may be your interview question. In a simple way we can use replace() but we don’t have to use it.
To solve this we get substring before character and substring after character then we combined. str1+char+str2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class ReplaceChar { public static void main(String[] args) { String s1 = " My Name is Joker"; char find = 'J'; char replace = 'P'; int i = s1.indexOf(find); String str1 = s1.substring(0, i); String str2 = s1.substring(i + 1); String s2 = str1 + replace + str2; System.out.println("Old String : " + s1); System.out.println("New String : " + s2); } } |
1 2 | Old String : My Name is Joker New String : My Name is Poker |
java replace string with another string
Here One String is replaced with another string using replaceAll().
1 2 3 4 5 6 7 8 | public class JavaSubstring { public static void main(String[] args) { String s1 = "This is a red doll"; String s2 = s1.replaceAll("red doll", "yellow shirt"); System.out.println("Old String :" + s1); System.out.println("New String :" + s2); } } |
1 2 | Old String :This is a red doll New String :This is a yellow shirt |
Replace vowels in a string java
Here we considered both upper and lower case vowels and replaced it with -. Here replaceAll() method is used to replace.
1 2 3 4 5 6 7 8 | public class JavaSubstring { public static void main(String[] args) { String s1 = "This is a red doll"; String s2 = s1.replaceAll("[AaEeIiOoUu]", "-"); System.out.println("Old String :" + s1); System.out.println("New String :" + s2); } } |
1 2 | Old String :This is a red doll New String :Th-s -s - r-d d-ll |
Java string replace whitespace | Replace whitespace in java
White spaces are spaces between characters and words it also includes \t \r \n. To remove white spaces we use the \\s pattern that includes all-white spaces.
1 2 3 4 5 6 7 8 | public class JavaSubstring { public static void main(String[] args) { String s1 = "T h i s i s a r e d d o l l"; String s2 = s1.replaceAll("\\s",""); System.out.println("Old String :" + s1); System.out.println("New String :" + s2); } } |
1 2 | Old String :T h i s i s a r e d d o l l New String :Thisisareddoll |