scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?
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]".rprintln("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]".rprintln("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字符串替換
總結
以上是生活随笔為你收集整理的scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 踩过界剧情介绍
- 下一篇: 原神风龙废墟怎么开启