×

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

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

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

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.