×

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

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.