带有示例的 Scala String replaceAll() 方法
replaceAll()方法用于用我们在参数列表中提供的字符串替换与正则表达式匹配的字符串的每个声明的子字符串。
Method Definition: String replaceAll(String regex, String replacement)
Return Type: It returns the stated string after replacing the stated sub-string with the string we provide.
示例 #1:
// Scala program of replaceAll()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying replaceAll method
val result = "csoNidhimso".replaceAll(".so", "##")
// Displays output
println(result)
}
}
输出:
##Nidhi##
示例 #2:
// Scala program of replaceAll()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying replaceAll method
val result = "csoNidhimsoSingh ".replaceAll(".so", "##")
// Displays output
println(result)
}
}
输出:
##Nidhi##Singh