Is Java a constant definition for any characters like SPACE?

Does Java include any single character constants like SPACE ?

Names taken from Unicode will be convenient when performing string manipulations.

I want it:

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

... and not this:

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

(should not be confused with the java.lang.Character class)

If nothing is related to Java, alternatives?

+5
source share
3 answers

There is no such constant and for good reason IMO.

Why use a constant primarily in your example?

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

less readable than

 String musician = "Lisa Coleman"; 

or even

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

Therefore, I think this is not for reasons of readability.

I assume that you want the constant not to repeat in several parts of the code. But using Character.SPACE instead of ' ' everywhere does not result in fewer repetitions. Only more verbose and less readable code.

So you are assuming that you want to change a constant value in one place and change it everywhere. But using the built-in Character.SPACE constant will not allow you to achieve this goal. You will still need your own constant, and its name should not be a value, but a value for :

 private static final char FIRST_NAME_LAST_NAME_SEPARATOR = ' '; 

Now there is a good reason to use this 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.

+8
source

Java has a list of character constants used to replace "non-programmable" characters, such as backspace (represented as \ r). A complete list can be found at https://docstore.mik.ua/orelly/java-ent/jenut/ch10_05.htm However, here are a few examples:

 \\ 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 mentioned symbolic constants in your name.

Edit: When using character constants as characters, they are surrounded by single quotes, not double quotes. Despite two (or more) characters, they represent one character or one binary digit, not a string.

Note. When trying to compare the 'enter' character in the java.awt.event.KeyListener keyTyped function, the \n character constant must be the user, not the \r character constant.

+1
source

There are some in Apache Commons

org.apache.commons.lang3.StringUtils.SPACE

more details

+1
source

Source: https://habr.com/ru/post/1233906/


All Articles