Open In App
Related Articles

Scala String replace() method with example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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


Last Updated : 03 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads