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.
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class string_example {
public static void main(String []args) {
String Strex = “Strings are supported in java by using String class”; // creating simple
Strex = “Java comes up with string class”;
System.out.println(“The actual string: “ + Strex);
System.out.println(“String after Java replace: “ + Strex.replace(‘c’, ‘C’));
}
}
|
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
Leave A Comment?