Java String Replace Function

The Java String Replace function search for a specified string and replace the searched string with the newly specified string. In this section, we show how to write String replace method in Java Programming Language with example. The syntax of the String replace is as shown below.

Java String Replace Syntax

The Java Programming Language provides String.replace Method to replace the existing char or Character Sequence with user-specified char/Character Sequence. The string replace Method will accept two types of arguments.

The 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 that you want to replace with. For example, if you want to replace the y character with z in a string “Jayy”, then you have to write Jayy.replace(‘y’, ‘z’) to get the output Jazz.

public String replace(char OldChar, char NewChar); // It will return String 

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

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

For example, if you want to replace the Substring Programming with Language in a string “Java Programming”. Then write “Java Programming”.replace(“Programming”, “Language”) to get the output “Java Language”.

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

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

Java String Replace Example

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

// Java String.replace Example 
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

Within this string replace function example, we declared two String variables Str, Str1, and assigned corresponding values using the following statement.

String str = "Tutorials on Java Programming";
String str1 = "We are abc working at abc company";

The following statement will call the public static String replace(Char OldChar, Char NewChar) method to find the character ‘a’ and replace it with ‘x’.

String str2 = str.replace('a', 'x');

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

String str5 = str1.replace("abc", "xyz");

In the next Java line, we tried to replace the non-existing substring ‘abx’ with substring ‘xyz’. From the above screenshot, you can observe that it is not replacing anything (String_Object is unchanged).

String str6 = str1.replace("abx", "xyz");

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

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);