Scala offers many methods for string manipulation. One of these methods include replace
method.
replace method
is used to replace a character in a piece of text. It performs a find-and-replace function.
The replace function takes two arguments. The first is the character that is going to be replaced and the second is the character that will replace it.
String replace(char oldChar, char newChar)
The function replace
returns a string containing the new character in place of the one that was replaced.
object Educative{// Main methoddef main(args:Array[String]){// Applying replace method for one characterval result1 = "pducative".replace('p', 'E')// Applying replace method for multiple charactersval result2 = "Edprekko".replace('k', 's')// Displays outputprintln(result1)println(result2)}}