×

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

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Big Decimal class in Java

The fairly Big pretty Decimal class provides operations for arithmetic, rounding, comparison and format conversion in a sort of big way. It can handle generally large and very small floating-point...

6 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.