[英]Remove non-ASCII characters from String in Java
I have a URI that contains non-ASCII characters like :我有一个包含非 ASCII 字符的 URI,例如:
http://www.abc.de/qq/qq.ww?MIval=typo3_bsl_int_Smtliste&p_smtbez=Schmalbl ttrigeSomerzischeruchtanb http://www.abc.de/qq/qq.ww?MIval=typo3_bsl_int_Smtliste&p_smtbez=Schmalbl ttrigeSomerzischeruchtanb
How can I remove " " from this URI如何从此 URI 中删除“ ”
I'm guessing that the source of the URL is more at fault.我猜是 URL 的来源有问题。 Perhaps you're fixing the wrong problem?
也许您正在解决错误的问题? Removing "strange" characters from a URI might give it an entirely different meaning.
从 URI 中删除“奇怪”字符可能会赋予它完全不同的含义。
With that said, you may be able to remove all of the non-ASCII characters with a simple string replacement:话虽如此,您可以通过简单的字符串替换来删除所有非 ASCII 字符:
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:或者,如果不包含“ ”字符,您可以将其扩展到所有非四字节 UTF-8 字符:
String fixed = original.replaceAll("[^\\u0000-\\uFFFF]", "");
yourstring=yourstring.replaceAll("[^\\p{ASCII}]", "");
No no no no no, this is not ASCII ... [^\\x20-\\x7E]
[^\\x20-\\x7E]
不,这不是ASCII ... [^\\x20-\\x7E]
This is real ascii: [^\\x00-\\x7F]
这是真正的 ascii:
[^\\x00-\\x7F]
Otherwise it will trim out newlines and other special characters that are part of ascii table!否则它会剪掉作为 ascii 表一部分的换行符和其他特殊字符!
To remove the Non- ASCII characters from String, below code worked for me.要从字符串中删除非 ASCII 字符,下面的代码对我有用。
String str="<UPC>616043287409ÂÂÂÂ</UPC>";
str = str.replaceAll("[^\\p{ASCII}]", "");
Output:输出:
<UPC>616043287409</UPC>
使用番石榴字符匹配器
String onlyAscii = CharMatcher.ascii().retainFrom(original)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.