[英]Java String class replaceAll method missing
我现在遇到一个很奇怪的问题, String
对象缺少replaceAll
方法。
JDK版本:1.8.0
Android Studio版本:3.1.3
Kotlin版本:1.2
它尚未被删除 ,所以这里发生了什么??
您只需在Kotlin中进行replace
:
"foo and foo".replace("foo", "bar") // "bar and bar"
请注意,上面的调用替换了文字字符串,如果您需要Regex
作为第一个参数,则可以执行以下任一操作:
"foo and bar".replace("[abcd]".toRegex(), "x") // "foo xnx xxr"
"foo and bar".replace(Regex("[abcd]"), "x") // "foo xnx xxr"
Kotlin有它自己的String类。 所有的替换方法都只是replace
。
Java中的replaceAll
使用正则表达式,因此我假设您的目标是使用正则表达式。 为此,只需在传递替换时使用正则表达式对象即可:
sb.toString().replace("your regex".toRegex(), "replacement");
如果没有正则表达式(那是错误的),请不要调用.toRegex()
:
sb.toString().replace("replace target", "replacement");
Kotlin将replaceAll替换为:
actual fun String.replace(
oldChar: Char,
newChar: Char,
ignoreCase: Boolean = false
): String (source)
返回一个新字符串,其中所有出现的oldChar都替换为newChar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.