Remove non-ASCII characters from String in Java

前端 未结 5 1273
渐次进展
渐次进展 2021-01-04 00:06

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

5条回答
  •  迷失自我
    2021-01-04 00:32

    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]", "");
    

提交回复
热议问题