Scala String replace() 方法与示例
replace()方法用于将字符串的旧字符替换为参数中指定的新字符。
Method Definition: String replace(char oldChar, char newChar)
Return Type: It returns the stated string after replacing the old character with the new one.
示例 #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)
}
}
输出:
nidhi
示例 #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)
}
}
输出:
Nid#i