Search code examples
javaregexspringasciinon-ascii-characters

Non ascii value symbols not getting printed


I have a string which contains some characters and symbols in which some of them have ascii code and some of them don't; I have tried the below code from which I am able to convert characters but not symbols

 String strValue = "Ã – string çöntäining nön äsçii çhäräçtérs couldn’t";
        String str = Normalizer.normalize(strValue, Normalizer.Form.NFD);
        System.out.println(str);
        System.out.println( str.replaceAll( "[^\\p{ASCII}]","") );

output is :

à – string çöntäining nön äsçii çhäräçtérs couldn’t
A  string containing non ascii characters couldnt

I also want "--" and "'" from the string value provided.

If i don't do the normalisation it converts my string to

? ? string ??nt?ining n?n ?s?ii ?h?r??t?rs couldn?t 

Solution

  • Just don't replace the wanted chars with "":

    String strValue = "Ã – string çöntäining nön äsçii çhäräçtérs couldn’t";
    String str = Normalizer.normalize(strValue, Normalizer.Form.NFD);
    System.out.println(str);
    System.out.println( str.replaceAll( "[^\\p{ASCII}–’]","") ); // ie. replace not (ascii or – or ’)
    

    Output:

    Ã – string çöntäining nön äsçii çhäräçtérs couldn’t
    A – string containing non ascii characters couldn’t
    

    demo: https://ideone.com/6zpYao