Difference between replace() and replaceAll() in java

In this post, we will see difference between String’s replace() and replaceAll() methods in java.

String’s replace() and replaceAll() both replace all occurences in the String.

String’s replace() takes either two chars or two CharSequences as arguments and it will replace all occurrences of char or String but replaceAll() method takes regex String as argument and replaces each substring of that matches given regex with replacement string. If you use wrong method, then it can introduce bug in the code. That’s why it is good to know difference between replace() and replaceAll() methods in java.

Let’s understand replace() and replaceAll() with the help of examples.

replace()

You should use replace if you want to replace one char with another or one String with another.

replace char with another char using replace()

Here is an example to replace one char with another using replace() method. Here we will use replace() method to replace char ‘p’ with char ‘b’

Output:

abble

As you can see, char ‘p’ got replaced with ‘b’ in the output.

replace String with another String using replace()

Here is an example to replace one String with another String using replace() method. Here we will use replace() method to replace String "java" with "Python"

Output:

Python C C++ Python

As you can see, String "Java" got replace with "Python" in the output.

replaceAll()

You should use String’s replaceAll() method if you want to use regular expressions.

replace any number with char

Here is example to replace any number with char using replaceAll() method. Here we will use regular expression to replace any number to char ‘a’

Output:

aaaaaa___

As you can see, all the numbers got replaced with char 'a' in the output.

remove all whitespace in the String

You can use replaceAll() method to remove all whitespaces in the String by passing required regular expression to it.

Here is an example:

Output:

JavaisProgramminglanguage

As you can see, String’s whicespaces got replaced with "" in the output.

That’s all about difference between replace and replaceAll methods in java.

Related Posts

  • Escape double quotes in String in Java
    19 January

    How to escape double quotes in String in java

    In this post, we will see how to escape double quotes in String in java. There are scenarios where you need to escape double quotes already present in the String. This generally happens while dealing with JSON file format or reading file data. Escape double quotes in java Double quotes characters can be escaped with […]

  • 24 December

    How to capitalize first letter in java

    Learn about how to capitalize first letter in java.

  • 24 April

    Add character to String in java

    In this post, we will see how to add character to String in java. There are multiple ways to add character to String. Add character to the start of String You can add character at start of String using + operator. [crayon-6041b1a7d0da2792484599/] Add character to the end of String You can add character at start […]

  • 24 March

    Initialize List of String in java

    In this post, we will see how to initialize List of String in java. Can you initialize List of String as below: [crayon-6041b1a7d1337533708533/] You can't because List is an interface and it can not be instantiated with new List(). You need to instantiate it with the class that implements the List interface. Here are the […]

  • 29 February

    Java long to String

    In this post, we will see how to convert long to String in java. There are lot of ways to convert long to String.Let’s see each one by one. Using Long.toString() You can use Long class toString() method to convert long to String. [crayon-6041b1a7d2022218354676/] In case, Long can be null and you don’t want to […]

  • 06 October

    Java remove last character from string

    Learn about how to remove last character from String in java using different ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.