在不使用正则表达式的情况下删除字符串中的子字符串(不能使用 replaceAll) [英] remove substring in a string without using regex (cannot use replaceAll)

查看:34
本文介绍了在不使用正则表达式的情况下删除字符串中的子字符串(不能使用 replaceAll)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除字符串中的一些子字符串(在大型数据集中).子字符串通常包含特殊字符,例如: ., ^,/,... 并且 replaceAll() 会将它们视为正则表达式的特殊字符,例如点将匹配任何字符,这并不是我真正想要的.

I need to remove some substrings in strings (in a large dataset). The substrings often contain special characters, like these: ., ^, /,... and replaceAll() would treat them as special characters for regex, such as a dot would match any character, which is not really what I want.

是否有其他函数可以在不将第一个参数视为正则表达式的情况下进行替换"?

Is there other functions to do the "replace" without treating the first argument as regex?

推荐答案

只需使用 String.replace().它的功能相同,但它处理内部转义特殊字符以避免您不必担心正则表达式.

Just use String.replace(). It functions the same way, but it deals with escaping the special characters internally to avoid you having to worry about regex.

文档

这篇关于在不使用正则表达式的情况下删除字符串中的子字符串(不能使用 replaceAll)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆