×

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

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

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

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Equilibrium Index of an Array in Java

An array's equilibrium index is the condition where the sum of items with lower and higher indices equals. For example, an array A=[-4,5 2,6,-5] where: a[0]=-4, a[1]=5, a[2]=2, a[3]=6, a[4]=-5 Now according...

4 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.