Scala String replace() method with example
The replace() method is used to replace the old character of the string with the new one which is stated in the argument.
Method Definition: String replace(char oldChar, char newChar)
Return Type: It returns the stated string after replacing the old character with the new one.
Example #1:
// Scala program of replace()// method // Creating objectobject GfG{ // Main methoddef main(args:Array[String]){ // Applying replace methodval result = "Nidhi".replace('N', 'n') // Displays outputprintln(result) }} |
Output:
nidhi
Example #2:
// Scala program of replace()// method // Creating objectobject GfG{ // Main methoddef main(args:Array[String]){ // Applying replace methodval result = "Nidhi".replace('h', '#') // Displays outputprintln(result) }} |
Output:
Nid#i


