Remove special characters in the string in java?

前端 未结 7 2165
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  旧时难觅i
    2021-01-15 08:57

    String str="owl@134_- abc";
    String s=str.replaceAll(" [^a-zA-Z_-]+ ", "");
    System.out.println(str);
    

    It will replace the special character and white spaces from a given string.

    Output will be: owlabc_-

提交回复
热议问题