Search code examples
javastringsubstringreplaceall

remove substring in a string without using regex (cannot use 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?


Solution

  • 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