Remove special characters in the string in java?

前端 未结 7 2115
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 08:28

How to remove special characters in the string except \"- _\". Now I use:

replaceAll(\"[^\\\\w\\\\s]\", \"\")

it remove all special charact

7条回答
  •  心在旅途
    2021-01-15 08:41

    Pattern pt = Pattern.compile("[^a-zA-Z0-9_-]");
        Matcher match = pt.matcher(c);
        while (match.find()) {
            String s = match.group();
            c = c.replaceAll("\\" + s, "");
        }
    

    Consider this

提交回复
热议问题