如何从我的 Java 字符串中删除版权和其他非 ASCII 字符?
How do I remove copyright and other non-ASCII characters from my Java string?
我正在使用 Java 6(此时不能升级)。我有一个包含以下值的 Java 字符串:
My Product Edition 2014©
最后一个符号是版权符号 (©)。当此字符串输出到我的终端时(在 Mac 10.9.5 上使用 bash),版权符号呈现为问号。
我想知道如何从我的字符串中删除将在我的终端上呈现为问号的所有字符。
如果你想删除特殊字符,你可以这样做:
String s = "My Product Edition 2014©";
s = s.replaceAll("[^\w\s]", "");
System.out.println(s);
输出:
My Product Edition 2014
这里 "right" 要做的是修复您的终端,因此它不会打印正方形。参见 How do you echo a 4-digit Unicode character in Bash? and try just echoing Unicode characters directly in your terminal. It may be as simple as ensuring your LANG
environment variable is set to UTF-8
(on my Mac, $LANG
is en_US.UTF-8
). You might also consider using a more full-featured terminal, like iTerm2。
如果你真的想去除 Java 中的非 ASCII 字符,有许多同样合理的方法,但我更喜欢 Guava's CharMatcher
,例如:
String stripped = CharMatcher.ASCII.retainFrom(original);
您可以使用 Pattern
to strip undesirable characters, but (as demonstrated by the confusion here) 它比使用 Guava 的开箱即用解决方案更麻烦。
你最好接受没有 "special character" 这样的概念。但是,有几个原因导致某些字符无法正确显示。
Java 将在内部以 UTF-16 编码保留所有字符串。打印字符串时,字符会转换为相应输出流或输出编写器的编码。不幸的是,java 运行时间试图变得聪明并使用所谓的 "default" 编码 除非 您明确要求特定编码。
这尤其伤害 Windows 用户,默认编码通常是一些古老的 Microsoft "code page"。我还没有找到我可以在哪里告诉 Windows 我不想要他们的 CP 850(只要你有德语键盘,这是默认设置)。
在漫长的 运行 中,当您养成以下习惯时,您会过得最好:
- 使用 UTF-8 编码打开所有输出流(或写入器)。不要使用 System.out/System.err.
- 确保您使用的终端可以处理 UTF-8。如果您使用 windows,请输入
chcp 65001
以将 cmd-window 的编码设置为 UTF-8,并使用可以呈现 UTF 字符的字体。
您可以使用 regEx 和 replaceAll()trim 除不可读的 ASCII 字符以外的所有字符
public static String asciiOnly(String unicodeString)
{
String asciiString = unicodeString.replaceAll("[^\x20-\x7E]", "");
return asciiString;
}
下面是正则表达式的解释"[^\x20-\x7E]"
:
^
- 不是
\x20
- 表示第一个可写 ASCII 字符 space 的十六进制值。
-
——表示为,即x20to
x7E
\x7E
- 表示 ~
的十六进制值,这是最后一个可写的 ASCII 字符
ASCII
我正在使用 Java 6(此时不能升级)。我有一个包含以下值的 Java 字符串:
My Product Edition 2014©
最后一个符号是版权符号 (©)。当此字符串输出到我的终端时(在 Mac 10.9.5 上使用 bash),版权符号呈现为问号。
我想知道如何从我的字符串中删除将在我的终端上呈现为问号的所有字符。
如果你想删除特殊字符,你可以这样做:
String s = "My Product Edition 2014©";
s = s.replaceAll("[^\w\s]", "");
System.out.println(s);
输出:
My Product Edition 2014
这里 "right" 要做的是修复您的终端,因此它不会打印正方形。参见 How do you echo a 4-digit Unicode character in Bash? and try just echoing Unicode characters directly in your terminal. It may be as simple as ensuring your LANG
environment variable is set to UTF-8
(on my Mac, $LANG
is en_US.UTF-8
). You might also consider using a more full-featured terminal, like iTerm2。
如果你真的想去除 Java 中的非 ASCII 字符,有许多同样合理的方法,但我更喜欢 Guava's CharMatcher
,例如:
String stripped = CharMatcher.ASCII.retainFrom(original);
您可以使用 Pattern
to strip undesirable characters, but (as demonstrated by the confusion here) 它比使用 Guava 的开箱即用解决方案更麻烦。
你最好接受没有 "special character" 这样的概念。但是,有几个原因导致某些字符无法正确显示。
Java 将在内部以 UTF-16 编码保留所有字符串。打印字符串时,字符会转换为相应输出流或输出编写器的编码。不幸的是,java 运行时间试图变得聪明并使用所谓的 "default" 编码 除非 您明确要求特定编码。
这尤其伤害 Windows 用户,默认编码通常是一些古老的 Microsoft "code page"。我还没有找到我可以在哪里告诉 Windows 我不想要他们的 CP 850(只要你有德语键盘,这是默认设置)。
在漫长的 运行 中,当您养成以下习惯时,您会过得最好:
- 使用 UTF-8 编码打开所有输出流(或写入器)。不要使用 System.out/System.err.
- 确保您使用的终端可以处理 UTF-8。如果您使用 windows,请输入
chcp 65001
以将 cmd-window 的编码设置为 UTF-8,并使用可以呈现 UTF 字符的字体。
您可以使用 regEx 和 replaceAll()trim 除不可读的 ASCII 字符以外的所有字符
public static String asciiOnly(String unicodeString)
{
String asciiString = unicodeString.replaceAll("[^\x20-\x7E]", "");
return asciiString;
}
下面是正则表达式的解释"[^\x20-\x7E]"
:
^
- 不是\x20
- 表示第一个可写 ASCII 字符 space 的十六进制值。-
——表示为,即x20to
x7E\x7E
- 表示~
的十六进制值,这是最后一个可写的 ASCII 字符