Java String replace Function

The Java replace function searches for a specified string and replaces the searched text with the newly specified one. This section shows how to write String replace in Java Programming Language with examples. The syntax is as shown below.

Java String replace Syntax

The Java String function replaces the existing char or Character Sequence with a user-specified char/Character Sequence. It will accept two types of arguments.

The Java String replace function will accept two character arguments. The first argument (OldChar) is the character existing in the String_Object, and the second argument (NewChar) is the character you want to substitute with. For example, if you want to change the y character with z in a string “Jayy”, then you must write Jayy.replace(‘y’, ‘z’) to get the output Jazz.

public String replace(char OldChar, char NewChar);

//In order to use in program
String_Object.replace(char OldChar, char NewChar);

It will accept two Character Sequence arguments; the first argument (target) is a substring existing in the String_Object. The second argument rep is a substring that you want to replace with.

public String replace(CharSequence target, CharSequence rep); // It will return String 

//In order to use in program
String_Object.replace(CharSequence target, CharSequence rep);
  • String_Object: Please specify the valid string on which you want to perform.
  • OldChar: Please specify the Character that you want to change. Whatever you place here, it will substitute with a new string (NewChar)
  • NewChar: Please specify the valid new Character you want to insert into String_Object using this String function.
  • Target: Please specify the valid string that you want to change. Whatever you place here, it will replace by a new string (Replacement).

Java String replace Example

In this program, we used the String Function to replace the existing substring with a user-specified new string in a string object.

Within this function example, Str2 will call the public static String replace (Char OldChar, Char NewChar) method to find the character ‘a’ and change it with ‘x’.

The following str5 Java statement calls the public static String replace (CharSquence Target, CharSquence Replacement) method to find the substring ‘abc’ and substitute it with ‘xyz’.

In the next Java line, we tried to substitute the non-existing substring ‘abx’ with the substring ‘xyz’. The above screenshot shows that it is not doing anything (String_Object is unchanged).

Lastly, we used the System.out.println statements to print the output.

package StringFunctions;

public class ReplaceMethod {
 public static void main(String[] args) {
 String str = "Tutorials on Java Programming";
 String str1 = "We are abc working at abc company";
 
 String str2 = str.replace('a', 'x');
 String str3 = str1.replace('a', 'x');
 String str4 = str.replace("o", "xy");
 String str5 = str1.replace("abc", "xyz");
 String str6 = str1.replace("abx", "xyz");

 System.out.println("After Replacing Char a with x in String str = " + str2);
 System.out.println("After Replacing Char a with x in String str1 = " + str3);
 System.out.println("After Replacing CharSequence a with xy in String str = " + str4);
 System.out.println("After Replacing CharSequence abc with xyz in String str1 = " + str5);
 System.out.println("After Replacing CharSequence abx with xyz in String str1 = " + str6);
 }
}
Java String Replace Function 1