×

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 Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

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.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 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.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.