Java 是否为任何字符(例如 SPACE)定义了常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33183525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:25:33  来源:igfitidea点击:

Does Java define constants for any characters such as SPACE?

javacharacterconstants

提问by Basil Bourque

Does Java include any constants for single characters such as SPACE?

Java 是否包含单个字符的任何常量,例如SPACE

Having names taken from Unicode would be handy when doing string manipulations.

在进行字符串操作时,取自 Unicode 的名称会很方便。

I want this:

我要这个:

String musician = "Lisa" + Character.SPACE + "Coleman" ;

…rather than this:

……而不是这个:

String musician = "Lisa" + " " + "Coleman" ;

(not to be confused with the java.lang.Characterclass)

(不要与java.lang.Character类混淆)

If nothing bundled with Java, alternatives?

如果没有与Java捆绑,替代品?

回答by JB Nizet

There is no such constant, and for good reasons, IMO.

没有这样的常数,并且有充分的理由,IMO。

Why use a constant in the first place in your example?

为什么在您的示例中首先使用常量?

String musician = "Lisa" + Character.SPACE + "Coleman" ;

is less readable than

可读性低于

String musician = "Lisa Coleman";

or even than

甚至比

String musician = "Lisa" + ' ' + "Coleman";

So I guess it's not for readability reasons.

所以我想这不是出于可读性原因。

I thus guess that you want a constant to avoid repeating yourself in several portions of code. But using Character.SPACEinstead of ' 'everywhere doesn't lead to less repetitions. Only to more verbose and less readable code.

因此,我猜想您需要一个常量以避免在代码的几个部分中重复自己。但是使用Character.SPACE而不是' '无处不在不会导致更少的重复。只是为了更冗长和可读性更低的代码。

I thus guess that you want to be able to change the constant value in one place, and have it changed everywhere it's used. But then using a built-in Character.SPACEconstant wouldn't allow you achieveing that goal. You would still need your own constant, and its name shouldn't be what the value is, but what the value is for:

因此,我猜测您希望能够在一个地方更改常量值,并在使用它的任何地方更改它。但是,使用内置Character.SPACE常量将无法实现该目标。你仍然需要自己定,它的名字不应该是值是什么,但价值是什么

private static final char FIRST_NAME_LAST_NAME_SEPARATOR = ' ';

Now, there is a good reason to use that constant: if you later decide to use a tab instead of a space, you can change the value of the constant and recompile all your code.

现在,有一个很好的理由使用该常量:如果您以后决定使用制表符而不是空格,则可以更改常量的值并重新编译所有代码。

回答by Samuel Newport

Java does have a list of character constants used to substitute "untypeable" characters such as a backspace (represented by \r). the full list can be found at https://docstore.mik.ua/orelly/java-ent/jenut/ch10_05.htmHowever here is a few examples:

Java 确实有一个字符常量列表,用于替换诸如退格符(由 \r 表示)之类的“无法键入”的字符。完整列表可以在https://docstore.mik.ua/orelly/java-ent/jenut/ch10_05.htm找到, 但这里有几个例子:

\      Backslash
\r      Carriage Return
\"      Double Quote (same for single Quote)
\n      Newline

You get the idea. Hope this is what you were looking for since you did mention character constants in your title.

你明白了。希望这是您正在寻找的内容,因为您确实在标题中提到了字符常量。

Edit: When using character constants as characters surround them with single quotes rather than double quotes. Despite being two (or more) characters long they denote a single character or a single binary digit rather than a string.

编辑:当使用字符常量作为字符时,用单引号而不是双引号将它们括起来。尽管有两个(或更多)字符长,但它们表示单个字符或单个二进制数字而不是字符串。

Note: When attempting to compare the 'enter' character in the java.awt.event.KeyListenerkeyTyped function the \ncharacter constant should be user rather than the \rcharacter constant.

注意:当尝试比较java.awt.event.KeyListenerkeyTyped 函数中的“输入”字符时,\n字符常量应该是用户而不是\r字符常量。

回答by Mykhaylo Adamovych

There are some in Apache Commons

Apache Commons 中有一些

org.apache.commons.lang3.StringUtils.SPACE

org.apache.commons.lang3.StringUtils.SPACE

more

更多的