Java 中两种不同的正斜杠

Two different Kinds of forward slashes in Java

在处理 Java 中的 unicode 编码字符时,我使用 Normalizer 对其进行规范化并将其转换为字符串。下面是我使用的代码:

input = "¼";
input = Normalizer.normalize(input,Normalizer.Form.NFKD);

output: 1⁄4. 

该方法使用的正斜杠是 "⁄",其 unicode 编码是 \u2044,而不是我可以使用键盘输入的常规正斜杠 "/"编码为 \u002f

它们之间有什么区别,什么时候应该用一个代替另一个?

提前致谢。

瑞施

如今的 Unicode 包含大量常见 non-letter 字符和 slashes are no exception 的变体。 (这甚至不是全部 - 搜索 "solidus" 以获得更多。)你有分数斜线(你的),full-width 斜线,除法斜线(是的,它与分数一分开), 粗斜杠, extra-thick 斜杠 - 不胜枚举。

好消息是您可以决定哪种斜杠适合您的上下文。

如果您只是因为不希望分数被压缩成单个字符,或者希望 所有 分数显示相同(unicode 显然可以每个可能的分数都有一个字符)然后使用这个分数斜线可能就是你想要的。

另一方面,如果您想要规范化是因为您希望将可用字符集减少到可以在标准键盘上轻松输入的字符集,那么您应该使用标准正斜杠。

正如 Michael Berry 提到的,\u2044fraction slash 字符。

不只是斜线看起来有点不同;它具有特定的渲染行为。来自 the Unicode specification, section 6.2, “Other Punctuation”:

Fraction Slash. U+2044 FRACTION SLASH is used between digits to form numeric fractions, such as 2/3 and 3/9. The standard form of a fraction built using the fraction slash is defined as follows: any sequence of one or more decimal digits (General Category = Nd), followed by the fraction slash, followed by any sequence of one or more decimal digits. Such a fraction should be displayed as a unit, such as ³⁄₄ or . The precise choice of display can depend on additional formatting information.

If the displaying software is incapable of mapping the fraction to a unit, then it can also be displayed as a simple linear sequence as a fallback (for example, 3/4). If the fraction is to be separated from a previous number, then a space can be used, choosing the appropriate width (normal, thin, zero width, and so on). For example, 1 + THIN SPACE + 3 + FRACTION SLASH + 4 is displayed as 1 ³⁄₄.

就个人而言,我更喜欢使用分数斜线,因为它使分数看起来更好,就像它们经过专业排版一样。但是在某些情况下,ASCII 斜杠更好,例如等宽文本,或者想要 all-ASCII 输出,或者正如 Michael 提到的那样,将文本限制为可以在键盘上键入的字符。