src/java.base/share/classes/java/lang/Character.java
Print this page
*** 10143,10153 ****
*/
public static String getName(int codePoint) {
if (!isValidCodePoint(codePoint)) {
throw new IllegalArgumentException();
}
! String name = CharacterName.get(codePoint);
if (name != null)
return name;
if (getType(codePoint) == UNASSIGNED)
return null;
UnicodeBlock block = UnicodeBlock.of(codePoint);
--- 10143,10153 ----
*/
public static String getName(int codePoint) {
if (!isValidCodePoint(codePoint)) {
throw new IllegalArgumentException();
}
! String name = CharacterName.getInstance().getName(codePoint);
if (name != null)
return name;
if (getType(codePoint) == UNASSIGNED)
return null;
UnicodeBlock block = UnicodeBlock.of(codePoint);
*** 10155,10160 ****
--- 10155,10203 ----
return block.toString().replace('_', ' ') + " "
+ Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
// should never come here
return Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
}
+
+ /**
+ * Returns the code point value of the Unicode character specified by
+ * the given Unicode character name.
+ * <p>
+ * Note: if a character is not assigned a name by the <i>UnicodeData</i>
+ * file (part of the Unicode Character Database maintained by the Unicode
+ * Consortium), its name is defined as the result of expression
+ *
+ * <blockquote>{@code
+ * Character.UnicodeBlock.of(codePoint).toString().replace('_', ' ')
+ * + " "
+ * + Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
+ *
+ * }</blockquote>
+ * <p>
+ * The {@code name} matching is case insensitive, with any leading and
+ * trailing whitespace character removed.
+ *
+ * @param name the Unicode character name
+ *
+ * @return the code point value of the character specified by its name.
+ *
+ * @exception IllegalArgumentException if the specified {@code name}
+ * is not a valid Unicode character name.
+ *
+ * @since 9
+ */
+ public static int codePointOf(String name) {
+ name = name.trim().toUpperCase(Locale.ENGLISH);
+ int cp = CharacterName.getInstance().getCodePoint(name);
+ if (cp != -1)
+ return cp;
+ try {
+ int off = name.lastIndexOf(' ');
+ if (off != -1) {
+ cp = Integer.parseInt(name, off + 1, name.length(), 16);
+ if (isValidCodePoint(cp) && name.equals(getName(cp)))
+ return cp;
+ }
+ } catch (Exception x) {}
+ throw new IllegalArgumentException("Unrecognized character name :" + name);
+ }
}