How to Replace a Function in Java
The Java programming language includes built-in capabilities for advanced processing of character strings. Those capabilities are encapsulated in the predefined "String" class. In particular, method "String.replace()" can replace all occurrences of a given character within the string with another character. You can call the replace function from your Java programs.
Instructions
-
-
1
Include the following line at the beginning of your Java code:
import Java.lang.String;
-
2
Assign the appropriate value to the String where the replacement will take place, as in the following sample code:
String initialString = "Localize and finalize the event";
-
-
3
Call String.replace() on the String, as in the following sample code:
String britString = initialString.replace('z','s');
Method String.replace() does not modify the String where the substitutions take place; it just returns a new String where the substitutions have been made. For the example, "britString" will have value "Localise and finalise the event".
-
1