Constants for special characters?

Is there any general library that provides constants for commonly used separator characters (eg StringUtils.SPACE/EMPTY/LF/CR ,;" ), similar to StringUtils.SPACE/EMPTY/LF/CR ?

+1
source share
1 answer

There are names, but not as constants.

  for (int cp = 32; cp < 48; ++cp) { System.out.printf("%c : %s%n", cp, Character.getName(cp)); } : SPACE ! : EXCLAMATION MARK " : QUOTATION MARK # : NUMBER SIGN $ : DOLLAR SIGN % : PERCENT SIGN & : AMPERSAND ' : APOSTROPHE ( : LEFT PARENTHESIS ) : RIGHT PARENTHESIS * : ASTERISK + : PLUS SIGN , : COMMA - : HYPHEN-MINUS . : FULL STOP / : SOLIDUS 

It cumbersomely works with constants, so I don’t think anyone has done their best.

You can do this yourself, perhaps by generating an enumeration using Character.getName at the top, replacing the empty name with an underscore.

 enum Chars { COMMA(','), SEMICOLON(';'), EXCLAMATION_SIGN('!'); private final int ch; private static Map<Integer, Chars> map = new HashMap<>(); static { for (Chars c : Chars.values()) { map.put(c.ch, c); } } private Chars(int ch) { this.ch = ch; } public int toCodePoint() { return ch; } public static Chars fromCodePoint(int cp) { return map.get(cp); } @Override public String toString() { return new String(new int[]{ ch }, 0, 1); } } 

Reason, there enumeration also has the name getter, for "COMMA", etc.

+1
source

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


All Articles