Java string replace method

The string replace method

The replace method of the String class is used to replace existing characters in a given string to the new given characters.

An example of replace method

The syntax of Java string replace method is:

Strex.replace(oldChar, newChar)

Where the Strex is an instance of the String object. The Java Replace method takes two parameters, one is the old character(s) and other contains new characters to be replaced with old character(s).

A string replace method example

The following example shows how to use the Java string replace method. In that example, we have created a string, which is assigned to a string object.

To see the difference, we have printed strings before and after using the replace, Java string method. The first line displays the actual string and the second line displays the string after using the replace method.

See the example by clicking the link below:

Experience this online


The output will be:

The actual string: Java comes up with string class

String after Java replace: Java Comes up with string Class

 

Also see: Java String

Was this article helpful?

Related Articles

Leave A Comment?