Open In App

Scala String replace() method with example

Last Updated : 03 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads