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 object object GfG { // Main method def main(args : Array[String]) { // Applying replace method val result = "Nidhi" .replace( 'N' , 'n' ) // Displays output println(result) } } |
Output:
nidhi
Example #2:
// Scala program of replace() // method // Creating object object GfG { // Main method def main(args : Array[String]) { // Applying replace method val result = "Nidhi" .replace( 'h' , '#' ) // Displays output println(result) } } |
Output:
Nid#i