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?
Yes, it is called
replace
:) Main difference between it andreplaceAll
is that it escapes regex special characters.BTW if you want to escape regex's special characters in string you can
yourString = Pattern.quote(yourString)
,"\\Q"
and"\\E"
,to escape only some special characters you can
"\\"
before them like\\.
"["
and"]"
like[.]
.Just use
String.replace(String, String)
, notreplaceAll
.String.replace
doesn't treat its argument as a regex.There are 2 methods named
replace
in theString
class that perform replacement without treating their parameters as regular expressions.One
replace
method replaces onechar
with anotherchar
.The other
replace
method replaces aCharSequence
(usually aString
) with anotherCharSequence
.Quoting the Javadocs from the second
replace
method:You can match literally. For instance, if we want to match "<.]}^", we can do:
and use that pattern.
You can also use backslashes to escape it. Note that the string literal itself needs backslashes, so escaping a single dot will take two backslashes, as follows:
First backslash is seen by compiler, and second backslash is taken as a backslash for the regex parser.
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.
Documentation