remove substring in a string without using regex (cannot use replaceAll)

前端 未结 5 1634
深忆病人
深忆病人 2021-01-28 04:32

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 s

5条回答
  •  走了就别回头了
    2021-01-28 05:16

    Is there other functions to do the "replace"

    Yes, it is called replace :) Main difference between it and replaceAll is that it escapes regex special characters.


    BTW if you want to escape regex's special characters in string you can

    • use yourString = Pattern.quote(yourString),
    • surround it with "\\Q" and "\\E",

    to escape only some special characters you can

    • use "\\" before them like \\.
    • also most special characters can be escaped by surrounding them with "[" and "]" like [.].

提交回复
热议问题