Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java String Replace Function

by suresh

The Java String Replace function is one of the Java String Method, which is used to search for specified string and replace the searched string with newly specified string.

In this article, we will show you, How to write String.replace method in Java Programming Language with example. Before we get into the example, the basic syntax of the String.replace in Java Programming language 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. Java String.replace Method will accept two types of arguments.

The String.replace function will accept two character arguments, 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 Java String.replace function will accept two Character Sequence arguments, first argument (target) is the substring existing in the String_Object, and the second argument (Replacement) is the 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 you can 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 Replacment.
  • OldChar: Please specify the Character that you want to change. Whatever you place here, Java String.replace Function will be replace it by new string (NewChar)
  • NewChar: Please specify the valid new Character you want to insert into String_Object.
  • Target: Please specify the valid string that you want to change. Whatever you place here, Java String.replace Function will be replace it by new string (Replacement)
  • Replacement: Please specify the valid new string you want to insert into String_Object.

Java String Replace Example

The Java String replace method is used to replace the existing substring with user specified new string in a string object. This Java program will help you to understand the Java string.replace method practically.

JAVA CODE

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

OUTPUT

Java String Replace Function 1

ANALYSIS

Within this string replace function example, First, we declared two String variables Str, Str1 and assigned corresponding values using 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 statement will call the public static String replace(CharSquence Target, CharSquence Replacement) method to find the sub string ‘abc’ and replace it with the substring ‘xyz’ .

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

In the next line, we are tried to replace the non-existing sub string ‘abx’ with sub string ‘xyz’ . From then 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 as shown below.

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

Thank You for Visiting Our Blog

Placed Under: Java Tutorial

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy