×

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 Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.