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)
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)
Kotlin 将 replaceAll 作为替换:
actual fun String.replace(
oldChar: Char,
newChar: Char,
ignoreCase: Boolean = false
): String (source)
Run Code Online (Sandbox Code Playgroud)
返回一个新字符串,其中所有出现的 oldChar 替换为 newChar。
| 归档时间: |
|
| 查看次数: |
5721 次 |
| 最近记录: |