Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java String replaceFirst Method

by suresh

The Java String replaceFirst method is one of the Java String Methods, which is to replace the first occurred substring with the newly specified string.

In this article, we show how to write the String.replaceFirst method in Java Programming Language with example. The basic syntax of the String.replaceFirst is as shown below.

Java String ReplaceFirst Syntax

The Java Programming Language provides String.replaceFirst Method to replace the first substring with user-specified String.

Java String.replaceFirst Method accepts two string arguments. The first argument (regex) is the regular expression that represents a substring existing in the String_Object, and the second argument (Replacement) is the substring that you want to replace with.

public String replaceFirst(String Regexp, String Replacement); // It will return String 

//In order to use in program
String_Object.replaceFirst(String Regexp, String Replacement);
  • String_Object: A valid string on which you want to perform Replacement.
  • Regexp: Please specify the valid string that you want to change or regular expression that represents the String. Whatever you place here, Java String.replaceFirst method will replace it by new string (Replacement)
  • Replacement: Please specify the valid new string you want to insert into String_Object.

Java String ReplaceFirst Example

The Java String.replaceFirst replaces the first occurred substring with user-specified new string in a string object. In this Java program, we use the substring as the first argument. It may help to understand the basic functionality of the replaceFirst String function.

// Java String.replaceFirst Example 
package StringFunctions;

public class ReplaceFirst {
	public static void main(String[] args) {
		String str = "Tutorials on Java Programming";
		String str1 = "We are abc working at abc company";
		
		String str2 = str.replaceFirst("a", "NEW");
		String str3 = str1.replaceFirst("a", "X");
		String str4 = str.replaceFirst("Java", "Java Script");
		String str5 = str1.replaceFirst("abc", "xyz");
		String str6 = str1.replaceFirst("abx", "xyz");

		System.out.println("After Replacing First String str = " + str2);
		System.out.println("After Replacing First String str1 = " + str3);
		System.out.println("After Replacing First String str = " + str4);
		System.out.println("After Replacing First String str1 = " + str5);
		System.out.println("After Replacing First String str1 = " + str6);
	}
}

OUTPUT

Java String replaceFirst Method 1

ANALYSIS

The following statement will call the public String replaceFirst(String Regexp, String Replacement) method to find the substring ‘a’ and replace it with ‘x’. If you observe the above screenshot, Although ‘a’ substring is repeated four times in Str variable, String.replaceFirst function is replacing the first occurrence

String str2 = str.replaceFirst("a", "NEW");

The following Java String replaceFirst statement will find the substring ‘abc’ and replace it with substring ‘xyz’ . From the above screenshot, Although ‘abc’ substring is repeated twice in Str variable, String.replaceFirst function is replacing the first occurrence.

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

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

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

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

String ReplaceFirst Example 2

This program helps to understand the use of regular expressions in string.replaceFirst method practically.

// Java String.replaceFirst Example 
package StringFunctions;

public class ReplaceFirst2 {
	public static void main(String[] args) {
		String str = "We are abc working at abc";
		String str1 = "Apple Cherry Apple";
		String strd = "Emp10s Emp20s Emp30s Emp40s";
		
		String str2 = str.replaceFirst("abc", "xyz");
		String str3 = str.replaceFirst("abc$", "xyz");
		String str4 = str1.replaceFirst("Apple", "Orange");
		String str5 = str1.replaceFirst("Apple$", "Orange");
		String str6 = str.replaceFirst("abc(.*)", "xyz");
		String str7 = str.replaceFirst("(.*)abc(.*)", "TutorialGateway.org");
		String str8 = strd.replaceFirst("Emp\\d\\ds", "Emp_ID 10 is Working in Sales Depat,");
		String str9 = strd.replaceFirst("Emp\\d\\ds$", ",Emp_ID 40 is Working in Sales Dept");

		System.out.println("After Replacing First Sub String in str = " + str2);
		System.out.println("After Replacing Last Sub String in str = " + str3);
		System.out.println("After Replacing First Sub String in str1 = " + str4);
		System.out.println("After Replacing Last Sub String in str1 = " + str5);
		System.out.println("After Replacing String str = " + str6);
		System.out.println("After Replacing String str = " + str7);
		System.out.println("After Replacing String strd = " + str8);
		System.out.println("After Replacing String strd = " + str9);
	}
}

OUTPUT

Java String replaceFirst Method 2

ANALYSIS

The following statement calls public String replaceFirst(String Regexp, String Replacement) method to find the first occurred substring ‘abc’ and replace it with substring ‘xyz’.

String str2 = str.replaceFirst("abc", "xyz");

The following Java String replaceFirst statement will find the last occurred substring ‘abc’ and replace it with substring ‘xyz’. See from the above, Although ‘abc’ substring repeated twice in Str variable, String.replaceFirst function is replacing the last occurred.

String str3 = str.replaceFirst("abc$", "xyz");

It will find the substring ‘abc’ and replace the ‘abc’, the string after the ‘abc’ with substring ‘xyz’.

String str6 = str.replaceFirst("abc(.*)", "xyz");

This String replaceFirst statement will find the substring ‘abc’ and replace the ‘abc’, the string before it, string after the ‘abc’ with substring ‘xyz’.

String str7 = str.replaceFirst("(.*)abc(.*)", "TutorialGateway.org");

The following statement will find the first occurred substring ‘Emp\\d\\ds’ and replace it with substring ‘Emp_ID 10 is Working in Sales Depat,”. Here, \\d\\ means integer value in between the Emp and ds.

String str8 = strd.replaceFirst("Emp\\d\\ds", "Emp_ID 10 is Working in Sales Depat,");

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

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

Home | About Us | Contact Us | Privacy Policy