Java - removing strange characters from a String

前端 未结 11 645
轮回少年
轮回少年 2020-12-10 03:04

How do I remove strange and unwanted Unicode characters (such as a black diamond with question mark) from a String?

Updated:

Please tell me the Unicode chara

11条回答
  •  醉梦人生
    2020-12-10 03:18

    You can use a String.replaceAll("[my-list-of-strange-and-unwanted-chars]","")

    There is no Character.isStrangeAndUnWanted(), you have to define what you want.

    If you want to remove control characters you can do

    String str = "\u0000\u001f hi \n";
    str = str.replaceAll("[\u0000-\u001f]", "");
    

    prints hi (keeps the space).

    EDIT If you want to know the unicode of any 16-bit character you can do

    int num = string.charAt(n);
    System.out.println(num);
    

提交回复
热议问题