日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

發布時間:2023/12/1 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中替换字符串中的正则表达式模式?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。