×

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string.

Syntax:

public String replaceAll(String regex, String replacement)

Parameters:

regex : regular expression

replacement : replacement sequence of characters

Returns:

a new String i.e. replaced String

Java String replaceAll() example 1: replace character

    public class JavaStringReplaceAllEx1
    { 
    public static void main(String args[])
    { 
    String s1="W3lcom3 to the TutorialsAndExampl3"; 
    String replaceString=s1.replaceAll("3","e");
    System.out.println(replaceString); 
       }   
    }

Output:

Welcome to the TutorialsAndExample

Java String replaceAll() example 2: replace word

public class JavaStringReplaceAllEx2
    { 
             public static void main(String args[])
             { 
    String s1="Welcome to rock TutorialAndExample"; 
    String replaceString=s1.replaceAll("rock","the");//replaces all occurrences of "rock" to "the" 
    System.out.println(replaceString); 
              }     
     }

Output:

Welcome to the TutorialAndExample

Java String replaceAll() example 3

public class JavaStringReplaceEx3 { 
    public static void main(String[] args) { 
        String str = "JABA IS THE BEST"; 
        String rs = str.replaceAll("JABA","JAVA");
        System.out.println(rs);
        String str1 = rs.concat(" PROGRAMMING LANGUAGE");
        System.out.println(str1);
        String rs2 = str1.replaceAll("THE BEST","A OBJECT ORIENTED");
        System.out.println(rs2);
    } 
}

Output:

JAVA IS THE BEST
JAVA IS THE BEST PROGRAMMING LANGUAGE
JAVA IS A OBJECT ORIENTED PROGRAMMING LANGUAGE

Java String replaceAll() example 4: remove white spaces

    public class JavaStringReplaceAllEx4
          { 
                 public static void main(String args[])
                 { 
                 String s1="tutorial and example  .com"; 
                 String replaceString=s1.replaceAll("\\s",""); 
                System.out.println(replaceString); 
          }     
       }

Output:

tutorialandexample.com

Related Topics

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

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

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.