Search code examples
javastringnon-ascii-charactersreplacealljava-6

ASCII Char remove not functioning


If a string contains any non-ASCII value, i need to to delete it.

I have tried replaceAll mrthod which is working fine with jdk 1.8 and above. but i want to deploy the same on jdk 1.6 and is it not functioning.

String Remarks2 ="hii:╘’i";

String Remarks = Remarks2.replaceAll("[^\\p{ASCII}]", "");
System.out.println("ans: "+Remarks);

Output in jdk 1.8: hii:i Output in jdk 1.6: hii:╘’i

Actual result must be- hii:i


Solution

  • As commented \p{ASCII} came later. As ASCII is the first 7-bits range:

    String remarks = remarks2.replaceAll("[^\u0000-\u007F]", "");
    

    You might try to normalize text decomposing é into e and a zero-width combining mark ´ first:

    remarks2 = Normalizer.normalize(remarks2, Form.NFD);