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

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

  • Java String contains IgnoreCase
    23 June

    Java String contains IgnoreCase

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusion We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String contains IgnoreCase checks. […]

  • Find and count occurences of substring in String in Java
    02 June

    Find and count occurrences of substring in string in java

    Table of ContentsUsing the indexOf() methodUsing regular expressionUsing split() methodUsing the countMatches() method from Apache Commons library In this post, we will see how to find and count occurrences of substring in string in java. Using the indexOf() method The indexOf() method in java is a specialized function to find the index of the first […]

  • Print double quotes in java
    23 May

    Print double quotes in java

    Table of ContentsPrint double quotes in java using backslash characterPrint double quotes in java using unicode characterPrint double quotes in java by appending double quote character 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-6107214b92dd7852103124/] When you print on console using System.out.println, you use double quotes and […]

  • Java Split String by Space
    13 May

    Java split string by space

    Learn about how to split String in java in different ways.

  • Split String by new line in java
    09 May

    How to split String by newline in java

    Learn about how to split String by newline in java

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.