我现在遇到一个非常奇怪的问题,replaceAll该String对象缺少该方法。JDK版本:1.8.0Android Studio版本:3.1.3Kotlin版本:1.2它尚未被删除,所以这里发生了什么??
3 回答
![?](http://img1.sycdn.imooc.com/54584c5e0001491102200220-100-100.jpg)
慕娘9325324
TA贡献1783条经验 获得超4个赞
您可以只replace在Kotlin中做到这一点:
"foo and foo".replace("foo", "bar") // "bar and bar"
请注意,上面的调用将替换文字字符串,如果您需要aRegex作为第一个参数,则可以执行以下任一操作:
"foo and bar".replace("[abcd]".toRegex(), "x") // "foo xnx xxr"
"foo and bar".replace(Regex("[abcd]"), "x") // "foo xnx xxr"
![?](http://img1.sycdn.imooc.com/5859e2d50001f6bb01000100-100-100.jpg)
qq_笑_17
TA贡献1818条经验 获得超7个赞
Kotlin有它自己的String类。所有的替换方法都是正义的replace
。
replaceAll
Java中使用正则表达式,因此我假设您的目标是使用正则表达式。为此,只需在传递替换时使用正则表达式对象即可:
sb.toString().replace("your regex".toRegex(), "replacement");
如果您没有正则表达式(那是错误的),请不要致电.toRegex()
:
sb.toString().replace("replace target", "replacement");
![?](http://img1.sycdn.imooc.com/533e4c2300012ab002200220-100-100.jpg)
慕哥9229398
TA贡献1877条经验 获得超6个赞
Kotlin将replaceAll替换为:
在科特林替换
actual fun String.replace(
oldChar: Char,
newChar: Char,
ignoreCase: Boolean = false
): String (source)
返回一个新字符串,其中所有出现的oldChar都替换为newChar。
添加回答
举报
0/150
提交
取消