String replace methods are used to replace a part of string with a new string.
We have 4 variations for replace in String
1 2 3 4 |
String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) String replaceAll(String regex, String replacement) String replaceFirst(String regex, String replacement) |
String replace(char oldChar, char newChar) returns a new String with the oldChar replaced with a newChar that was specified.
String replace(CharSequence target, CharSequence replacement) returns a new String by replacing a sequence of characters that are specified.
String replaceAll(String regex, String replacement) returns a new String by replacing all the matching regular expression with a new replacement String.
String replaceFirst(String regex, String replacement) returns a new String by replacing only the first matching regular expression with a new replacement String.
String replace methods example
1. replace() example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kscodes.sampleproject; public class StringReplaceExample { public static void main(String[] args) { String testStr1 = "We have so many characters to replace"; System.out.println("Using replace(char,char) to replace all 'a' with '$'"); System.out.println("Original String ::" + testStr1); System.out.println("Replaced String ::" + testStr1.replace('a', '$')); System.out.println("----------------------------------------------------"); String testStr2 = "I will learn java. I will code in java"; CharSequence cs1 = "will"; CharSequence cs2 = "did"; System.out.println("Using replace(CharSequence ,CharSequence ) to replace all 'will' with 'did'"); System.out.println("Original String ::" + testStr2); System.out.println("Replaced String ::" + testStr2.replace(cs1,cs2)); } } |
Output
1 2 3 4 5 6 7 |
Using replace(char,char) to replace all 'a' with '$' Original String ::We have so many characters to replace Replaced String ::We h$ve so m$ny ch$r$cters to repl$ce ---------------------------------------------------- Using replace(CharSequence ,CharSequence ) to replace all 'will' with 'did' Original String ::I will learn java. I will code in java Replaced String ::I did learn java. I did code in java |
2. replaceAll() example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kscodes.sampleproject; public class StringReplaceAllExample { public static void main(String[] args) { String testStr1 = "We will replace 12345. Also we will replace 111 and also 345"; System.out.println("Using replaceAll to replace all numbers with '@@'"); System.out.println("Original String ::" + testStr1); System.out.println("Replaced String ::" + testStr1.replaceAll("[0-9]+", "@@")); System.out.println("------------------------------------------------"); String testStr2 = "This is to test replaceAll for String"; System.out.println("Using replaceAll to replace all occureneces of 'is' with 00"); System.out.println("Original String ::" + testStr2); System.out.println("Replaced String ::" + testStr2.replaceAll("is", "00")); } } |
Output
1 2 3 4 5 6 7 |
Using replaceAll to replace all numbers with '@@' Original String ::We will replace 12345. Also we will replace 111 and also 345 Replaced String ::We will replace @@. Also we will replace @@ and also @@ ------------------------------------------------ Using replaceAll to replace all occureneces of 'is' with 00 Original String ::This is to test replaceAll for String Replaced String ::Th00 00 to test replaceAll for String |
3. replaceFirst() example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kscodes.sampleproject; public class StringReplaceFirstExample { public static void main(String[] args) { String testStr1 = "We will replace 12345. Also we will replace 111 and also 345"; System.out.println("Using replaceFirst to replace all numbers with '@@'"); System.out.println("Original String ::" + testStr1); System.out.println("Replaced String ::" + testStr1.replaceFirst("[0-9]+", "@@")); System.out.println("------------------------------------------------"); String testStr2 = "This is to test replaceAll for String"; System.out.println("Using replaceFirst to replace first occurenece of 'is' with 00"); System.out.println("Original String ::" + testStr2); System.out.println("Replaced String ::" + testStr2.replaceFirst("is", "00")); } } |
Output
1 2 3 4 5 6 7 |
Using replaceFirst to replace all numbers with '@@' Original String ::We will replace 12345. Also we will replace 111 and also 345 Replaced String ::We will replace @@. Also we will replace 111 and also 345 ------------------------------------------------ Using replaceFirst to replace first occurenece of 'is' with 00 Original String ::This is to test replaceAll for String Replaced String ::Th00 is to test replaceAll for String |
Reference
1. String API