×

Java String replaceFirst() method

This method replace the first substring with user specified String.

Syntax:

public String replaceFirst(String Regexp, String Replacement)

Parameter:

Regexp : Regular Expression

Replacement : the String which would replace found expression

Return: a string

Java String replaceFirst() Example 1

public class JavaStringReplaceFirstEx1
{
   public static void main(String args[])
   {
      String s1 = new String("example.com");
      String s2 = s1.replaceFirst("(.*)example(.*)", "tutorialandexample.com");
      System.out.println(s2);     
   }
}

Output:

tutorialandexample.com

Java String replaceFirst() Example 2

public class JavaStringReplaceFirstEx2{
   public static void main(String args[]){
       String str = new String("The replaceFirst method Tutorial");
      System.out.println(str.replaceFirst("The", "Java String"));//replacing The with Java String
   }
}

Output:

Java String replaceFirst method Tutorial

Java String replaceFirst() Example 3

import java.io.*;
public class JavaStringReplaceFirstEx3
{
   public static void main(String args[])
   {
      String Str = new String("Welcome to tutorial.com");
      System.out.println();
      System.out.print("Return Value :" );
      System.out.println(Str.replaceFirst("(.*)tutorial(.*)", "java Programming"));
      System.out.print("Return Value :" );
      System.out.println(Str.replaceFirst("tutorial", " tutorialandexample"));
      System.out.println();
   }
}

Output:

Return Value :java Programming

Return Value :

Welcome to tutorialandexample.com

Java String replaceFirst() Example 4

public class JavaStringReplaceFirstEx4
{
   public static void main(String args[])
   {
       String str = new String("tutorialandexample.com");
       System.out.println("Site name:" +str);
       System.out.println();
       System.out.print("String after replacing Site name:" );
       System.out.println(str.replaceFirst("com", "net"));    
   }


}

Output:

Site name:tutorialandexample.com
String after replacing Site name:tutorialandexample.net

Related Topics

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.