Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Replace in Java: Everything you Need to Know

Published on Oct 14,2019 1.1K Views


Replacing a number or string of characters is an interesting thing to do, be it Java, Python or any other programming language. In this article, however, we will focus on Replace in Java in the following order:

 

Types of Replace in Java

There are three methods of replacing Java String.

  • Replace
  • ReplaceAll
  • ReplaceFirst

Replace in Java

With the help of these options, you can replace any character in a string.

 

Replace in Java

String replace(): This method returns a new string as output by replacing every occurrence of the character with a new Character.

Syntax:

public Str replace(char oldC, char newCh)

Parameters:

oldCh − old character

newCh − new character

Return Value:

This replaces oldCh with newCh in the string.

Code:

public class Ex1 {
    public static void main(String args[]) {
        String S1 = new String("the quick fox jumped");
        System.out.println("Original String is ': " + S1);
        System.out.println("String after replacing 'cat' with 'dog': " + S1.replace("cat", "dog"));
        System.out.println("String after replacing all 't' with 'a': " + S1.replace('t', 'a'));

    }
}

Output:

Original String is ': the cat jumped
String after replacing ‘cat' with 'dog': the dog jumped
String after replacing all’t’ with 'a': ahe quick fox jumped

 

ReplaceAll

Java String Replaceall(): This method returns a new string replacing all the sequence of characters matching a regular expression and the replacement string.

Syntax:

public Str replaceAll(String regex, String replacement)

Parameters:

regx: regular expression
replacement: replacement sequence of characters

Code:

public class Ex2 {
    public static void main(String args[]) {
        String str = "Java web replace method";
        //remove white spaces
        String str2 = str.replaceAll("s", "");
        System.out.println(str2);
    }
}

Output:

Javewebreplacemethod

 

ReplaceFirst

Java String replaceFirst(): This method replaces the first substring of any given string which matches that regular expression.

Syntax:

public Str replaceFirst(String rgex, String replacement)

Parameters:

rgex − the regular expression to which given string needs to matched. 

replacement − the string that replaces regular expression.

Code:

public class Ex3 {
    public static void main(String args[]) {
        String str = "This is an example of replace";
        //Only Replace first 'i' with '7' 
        String str1 = str.replaceFirst("i", "7");
        System.out.println(str1);
    }
}

Output:

Th7s is an example of replace.

 

With this, we come to the end of this article. I hope you got an understanding of how to replace strings and characters.

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Replace in Java: Everything you Need to Know

edureka.co