Java String Replace

Java String replace method replaces all occurrences of old char to new char or old CharSequence to new CharSequence and return new String. If there is nothing to replace in the String, it will return same String.

Let’s say you need to convert "Java2blog" to "JavaTwoblog", you can simply use below syntax.

Syntax

There are two overloaded version of String’s replace method.

Example

Let’s understand with the help of Simple example.

When you run above program, you will get below output:

HelloWorld converted to : HellnWnrld

Above program will call public String replace(char oldChar, char newChar) version of replace method.

When you run above program, you will get below output:

How HashSet works in java

Above program will call public String replace(char oldChar, char newChar) version of replace method.

Internal implementation

public String replace(char oldChar, char newChar)

As you can see, above method finds first occurence of oldChar and then remaining string and replaces it with newChar.

public String replace(CharSequence target, CharSequence replacement)

Above code used Pattern and matcher to replace String.
That’s all about Java String replace method.

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.

  • 06 December

    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 […]

  • 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-604695e7b8410696862238/] 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-604695e7b872c242966591/] 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-604695e7b95c9279264186/] In case, Long can be null and you don’t want to […]

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.