×

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 : old Character

newChar : new Character

target : target sequence of characters

replacement: replacement sequence of characters

Returns:

a new String i.e. replaced Strin

Java String replace(char old, char new) method example 1

    public class JavaStringReplaceEx1
    { 
    public static void main(String args[])
    { 
    String s1="W3lcom3 to the TutorialsAndExampl3"; 
    String replaceString=s1.replace('3','e');//replaces all occurrences of '3' to 'e' 
    System.out.println(replaceString); 
       }    
    }

Output:

Welcome to the TutorialsAndExample

Java String replace(CharSequence target, CharSequence replacement) method example 2

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

Output:

Welcome to the TutorialAndExample

Java String replace method Example 3

public class JavaStringReplaceEx3 { 
    public static void main(String[] args) { 
        String str = "JABA IS THE BEST"; 
        String rs = str.replace("JABA","JAVA");
        System.out.println(rs);
        String str1 = rs.concat(" PROGRAMMING LANGUAGE");
        System.out.println(str1);
        String rs2 = str1.replace("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 replace method Example 4

public class JavaStringReplaceEx4 { 
    public static void main(String[] args) { 
       String a = "HelloBrother How are you!";
       String r = a.replace("HelloBrother","Brother");
       System.out.println(r);
    } 
}

Output:

Brother How are you!

Related Topics

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

Cyclic Barrier in Java

Programmers often find it challenging to run multiple threads simultaneously. Java introduces the idea of concurrency, which enables us to run many threads concurrently, making this work simpler. Concurrent programming...

6 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.