×

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

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

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

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.