Java字符串类replaceAll方法丢失

dak*_*aka 11 java android kotlin

我现在遇到一个很奇怪的问题,replaceAllString对象缺少该方法。

JDK版本:1.8.0
Android Studio版本:3.1.3
Kotlin版本:1.2

在此处输入图片说明

它尚未被删除,所以这里发生了什么??

zsm*_*b13 18

您可以只replace在Kotlin中做到这一点:

"foo and foo".replace("foo", "bar") // "bar and bar"
Run Code Online (Sandbox Code Playgroud)

请注意,以上调用将替换文字字符串,如果您需要a Regex作为第一个参数,则可以执行以下任一操作:

"foo and bar".replace("[abcd]".toRegex(), "x") // "foo xnx xxr"
"foo and bar".replace(Regex("[abcd]"), "x")    // "foo xnx xxr"
Run Code Online (Sandbox Code Playgroud)


Zoe*_*Zoe 9

Kotlin有它自己的String类。所有的替换方法都是正义的replace

replaceAllJava中使用正则表达式,因此我假设您的目标是使用正则表达式。为此,只需在传递替换时使用正则表达式对象即可:

sb.toString().replace("your regex".toRegex(), "replacement");
Run Code Online (Sandbox Code Playgroud)

如果您没有正则表达式(那是错误的),请不要致电.toRegex()

sb.toString().replace("replace target", "replacement");
Run Code Online (Sandbox Code Playgroud)


Zin*_*inc 6

Kotlin 将 replaceAll 作为替换:

在 Kotlin 中替换

actual fun String.replace(
oldChar: Char, 
newChar: Char, 
ignoreCase: Boolean = false
): String (source)
Run Code Online (Sandbox Code Playgroud)

返回一个新字符串,其中所有出现的 oldChar 替换为 newChar。