Java String replaceFirst() Method

Java String replaceFirst Method

Strings in Java 

In Java, A group of characters known as a string class are ones that a script interprets literally. A string is represented as a byte (or term) range information structure in order to preserve a number of components, frequently letters. Additional types and arrangements of sequence (or array) data, such as more adaptable arrays, may also be designated by the sequence.

  • Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.

To know more about String Class in java read the complete article.

String replaceFirst()Method:

One of the Java String Methods is the replaceFirst method, which replaces the first occurring substring with a freshly provided string. The Java String replaceFirst Method substitutes a new string given by the user for the first substring that appears in an object.

  • The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

Syntax:

public String replaceFirst(String regex, String replacement)

Parameters:

regex - the regular expression to which this string is to be matched
replacement - the string to be substituted for the first match

Let’s look at a string-related Java program where the Java String replaceFirst() Method is used to perform an operation on the given string.

Example: ReplaceFirst() Method

public class Main {
  public static void main(String[] args) {
    String myStr = "Hello";
    System.out.println(myStr.replace('H', 'p'));
  }
}

Output

pello

Example 2 : Replacing char from the string

Run
public class Main {
  public static void main(String[] args) {  
 String str = "PrepInsta Prime";  
        System.out.println("Returning words:");  
        String[] arr = str.split("P", 0);  
        for (String w : arr) {  
            System.out.println(w);  
        }  
        System.out.println("Split array length: "+arr.length);  
    }  
}

Output

Hey I am Prepxtar
Hey I am zrepStar

Example 3: Replacing char from the string

Run
public class Main {
  public static void main(String[] args) {
    String myStr = "My PrepInsta Prime is account is of one xear";
    System.out.println(myStr.replace('x', 'y'));
  }
}

Output

My PrepInsta Prime is account is of one year

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription