Java String replace() method returns the string replacing all the old char or CharSequence to a new char or CharSequence. At times, it is required to replace some characters in a String with a new character. It might also be required to change an entire sequence of characters with a new sequence of characters, i.e., replacing an old CharSequence with a new CharSequence. The java.lang.String.replace() method is a built-in method of the String class designed just for this purpose.
Java string replace() method returns the new string resulting from replacing all occurrences of old characters in the string with new characters.
There are two types of replace() methods in Java String class.
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
The second replace() method is added since JDK 1.5.
NullPointerException: if the replacement or target is equal to null.
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}
public String replace(CharSequence target, CharSequence replacement)
{
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
FileName: ReplaceExample1.java
public class ReplaceExample1{
public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'
System.out.println(replaceString);
}}
Output:
jevetpoint is e very good website
FileName: ReplaceExample2.java
public class ReplaceExample2{
public static void main(String args[]){
String s1="my name is khan my name is java";
String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was"
System.out.println(replaceString);
}}
Output:
my name was khan my name was java
FileName: ReplaceExample3.java
public class ReplaceExample3 {
public static void main(String[] args) {
String str = "oooooo-hhhh-oooooo";
String rs = str.replace("h","s"); // Replace 'h' with 's'
System.out.println(rs);
rs = rs.replace("s","h"); // Replace 's' with 'h'
System.out.println(rs);
}
}
Output:
oooooo-ssss-oooooo
oooooo-hhhh-oooooo
The replace() method throws the NullPointerException when the replacement or target is null. The following example confirms the same.
FileName: ReplaceExample4.java
public class ReplaceExample4
{
// main method
public static void main(String argvs[])
{
String str = "For learning Java, JavaTpoint is a very good site.";
int size = str.length();
System.out.println(str);
String target = null;
// replacing null with JavaTpoint. Hence, the NullPointerException is raised.
str = str.replace(target, "JavaTpoint ");
System.out.println(str);
}
}
Output:
For learning Java, JavaTpoint is a very good site.
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.String.replace(String.java:2142)
at ReplaceExample4.main(ReplaceExample4.java:12)
#java #replace()