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 in Java Programming Language with example. The syntax is as shown below.

Java String replace Syntax

The Java String function is to replace the existing char or Character Sequence with 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 that you want to substitute with. For example, if you want to change 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);

//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 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 it by a new string (Replacement).

Java String replace Example

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

Within this Java string 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 string statement call the public static String replace (CharSquence Target, CharSquence Replacement) method to find the substring ‘abc’ and substituted it with ‘xyz’.

In the next Java line, we tried to substitute the non-existing substring ‘abx’ with substring ‘xyz’. From the above screenshot, you can observe 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