简体   繁体   中英

Remove non-ASCII characters from String in Java

I have a URI that contains non-ASCII characters like :

http://www.abc.de/qq/qq.ww?MIval=typo3_bsl_int_Smtliste&p_smtbez=Schmalbl ttrigeSomerzischeruchtanb

How can I remove " " from this URI

I'm guessing that the source of the URL is more at fault. Perhaps you're fixing the wrong problem? Removing "strange" characters from a URI might give it an entirely different meaning.

With that said, you may be able to remove all of the non-ASCII characters with a simple string replacement:

String fixed = original.replaceAll("[^\\x20-\\x7e]", "");

Or you can extend that to all non-four-byte-UTF-8 characters if that doesn't cover the " " character:

String fixed = original.replaceAll("[^\\u0000-\\uFFFF]", "");
yourstring=yourstring.replaceAll("[^\\p{ASCII}]", "");

No no no no no, this is not ASCII ... [^\\x20-\\x7E]

This is real ascii: [^\\x00-\\x7F]

Otherwise it will trim out newlines and other special characters that are part of ascii table!

To remove the Non- ASCII characters from String, below code worked for me.

String str="<UPC>616043287409ÂÂÂÂ</UPC>";

str = str.replaceAll("[^\\p{ASCII}]", "");

Output:

<UPC>616043287409</UPC>

使用番石榴字符匹配器

String onlyAscii = CharMatcher.ascii().retainFrom(original)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM