非 ascii 值符号未打印
Non ascii value symbols not getting printed
我有一个包含一些字符和符号的字符串,其中一些有 ascii 码,一些没有;我已经尝试了下面的代码,我可以从中转换字符而不是符号
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}]","") );
输出是:
à – string çöntäining nön äsçii çhäräçtérs couldn’t
A string containing non ascii characters couldnt
我还需要提供的字符串值中的“--
”和“'
”。
如果我不进行规范化,它会将我的字符串转换为
? ? string ??nt?ining n?n ?s?ii ?h?r??t?rs couldn?t
只是不要用 ""
:
替换想要的字符
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 ’)
输出:
à – string çöntäining nön äsçii çhäräçtérs couldn’t
A – string containing non ascii characters couldn’t
如果你想具体替换那些字符,你可以先替换它们。
str = str.replaceAll("’", "'");
str = str.replaceAll("–", "--");
str = str.replaceAll( "[^\p{ASCII}–’]","");
我有一个包含一些字符和符号的字符串,其中一些有 ascii 码,一些没有;我已经尝试了下面的代码,我可以从中转换字符而不是符号
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}]","") );
输出是:
à – string çöntäining nön äsçii çhäräçtérs couldn’t
A string containing non ascii characters couldnt
我还需要提供的字符串值中的“--
”和“'
”。
如果我不进行规范化,它会将我的字符串转换为
? ? string ??nt?ining n?n ?s?ii ?h?r??t?rs couldn?t
只是不要用 ""
:
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 ’)
输出:
à – string çöntäining nön äsçii çhäräçtérs couldn’t
A – string containing non ascii characters couldn’t
如果你想具体替换那些字符,你可以先替换它们。
str = str.replaceAll("’", "'");
str = str.replaceAll("–", "--");
str = str.replaceAll( "[^\p{ASCII}–’]","");