scala字符串替换
Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string)
Replacing a part of the string that matches our given regular expression can be done using multiple methods.
可以使用多种方法替换匹配给定正则表达式的字符串部分 。
As strings are immutable you cannot replace the pattern in the string itself instead, we will be creating a new string that stores the updated string.
由于字符串是不可变的,因此您不能替换字符串本身中的模式,我们将创建一个新字符串来存储更新后的字符串。
1)replaceAll()方法 (1) replaceAll() Method)
The method replaces all the occurrences of the pattern matched in the string.
该方法替换字符串中所有匹配的模式。
Syntax:
句法:
string.replaceAll("regex", "replaceString")
Program to replace regular expression pattern in a string using replaceAll()
程序使用replaceAll()替换字符串中的正则表达式模式
object MyClass {
def main(args: Array[String]) {
val myString = "i can ride at a speed of 190 KmpH"
println("The string is '" + myString + "'")
println("Replacing all digits of the string with '*'")
val updatedString = myString.replaceAll("[0-9]+", "*")
println("Updated string is' " + updatedString + "'")
}
}
Output
输出量
The string is 'i can ride at a speed of 190 KmpH'
Replacing all digits of the string with '*'
Updated string is' i can ride at a speed of * KmpH'
2)replaceAllIn()方法 (2) replaceAllIn() Method)
The method does the same work as replaceAll() but has a different syntax.
该方法的功能与replaceAll()相同,但是语法不同。
Syntax:
句法:
regex.replaceAllIn("String", "replacestring")
The function also returns an updated string with the matched pattern changed with the given replacestring.
该函数还返回一个更新的字符串,其中匹配的模式已更改为给定的replacestring 。
Program to replace regular expression pattern in a string using replaceAllIn()
程序使用replaceAllIn()替换字符串中的正则表达式模式
object MyClass {
def main(args: Array[String]) {
val myString = "I can ride At the Speed of 190 KmpH"
val pattern = "[A-Z]".r
println("The string is '" + myString + "'")
println("Replacing all uppercase characters of the string with '*'")
val updatedString = pattern.replaceAllIn(myString, "*")
println("Updated string is '" + updatedString + "'")
}
}
Output
输出量
The string is 'I can ride At the Speed of 190 KmpH'
Replacing all uppercase characters of the string with '*'
Updated string is '* can ride *t the *peed of 190 *mp*'
3)replaceFirst()方法 (3) replaceFirst() Method)
The replaceFirst() method will replace the match pattern in the string but only at its first occurrence i.e. the match pattern will be replaced only at its first occurrence.
replaceFirst()方法将替换字符串中的匹配模式,但仅在其首次出现时进行替换,即,匹配模式将仅在其首次出现时进行替换。
Syntax:
句法:
string.replaceFirst("regex", "replaceString")
The function will return an updated string with the first matched pattern replaced with the given replace string.
该函数将返回一个更新的字符串,其中第一个匹配的模式将替换为给定的替换字符串。
Program to replace regular expression pattern in a string using replaceFirst()
程序使用replaceFirst()替换字符串中的正则表达式模式
object MyClass {
def main(args: Array[String]) {
val myString = "i can ride at a speed of 190 KmpH"
println("The string is '" + myString + "'")
println("Replacing first digits of the string with '*'")
val updatedString = myString.replaceFirst("[0-9]", "*")
println("Updated string is '" + updatedString + "'")
}
}
Output
输出量
The string is 'i can ride at a speed of 190 KmpH'
Replacing first digits of the string with '*'
Updated string is 'i can ride at a speed of *90 KmpH'
4)replaceFirstIn()方法 (4) replaceFirstIn() Method)
The replaceFirstIn() method will replace the match pattern in the string but only at its first occurrence i.e. the match pattern will be replaced only at its first occurrence.
replaceFirstIn()方法将替换字符串中的匹配模式,但仅在其首次出现时进行替换,即,匹配模式将仅在其首次出现时进行替换。
The only difference is the syntax.
唯一的区别是语法。
Syntax:
句法:
regex.replaceFirstIn("string", "replaceString")
Program to replace regular expression pattern in a string using replaceFirstIn()
程序使用replaceFirstIn()替换字符串中的正则表达式模式
object MyClass {
def main(args: Array[String]) {
val myString = "I can ride At the Speed of 190 KmpH"
val pattern = "[A-Z]".r
println("The string is '" + myString + "'")
println("Replacing first occurence of uppercase characters of the string with '*'")
val updatedString = pattern.replaceFirstIn(myString, "*")
println("Updated string is '" + updatedString + "'")
}
}
Output
输出量
The string is 'I can ride At the Speed of 190 KmpH'
Replacing first occurence of uppercase characters of the string with '*'
Updated string is '* can ride At the Speed of 190 KmpH'
翻译自: https://www.includehelp.com/scala/how-to-replace-a-regular-expression-pattern-in-a-string-in-scala.aspx
scala字符串替换