Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does String.codePointAt do?

Recently I ran into codePointAt method of String in Java. I found also a few other codePoint methods: codePointBefore, codePointCount etc. They definitely have something to do with Unicode but I do not understand it.

Now I wonder when and how one should use codePointAt and similar methods.

like image 795
Michael Avatar asked Sep 05 '12 11:09

Michael


People also ask

What is the difference between charCodeAt and codePointAt in JavaScript?

Difference Between charCodeAt() and codePointAt()charCodeAt() is UTF-16, codePointAt() is Unicode. charCodeAt() returns a number between 0 and 65535. Both methods return an integer representing the UTF-16 code of a character, but only codePointAt() can return the full value of a Unicode value greather 0xFFFF (65535).

What is code point in JavaScript?

In JavaScript, fromCodePoint() is a string method that is used to create a string from a sequence of Unicode code points (that may not be representable in a single UTF-16 code unit).


1 Answers

Short answer: it gives you the Unicode codepoint that starts at the specified index in String. i.e. the "unicode number" of the character at that position.

Longer answer: Java was created when 16 bit (aka a char) was enough to hold any Unicode character that existed (those parts are now known as the Basic Multilingual Plane or BMP). Later, Unicode was extended to include characters with a codepoint > 216. This means that a char could no longer hold all possible Unicode codepoints.

UTF-16 was the solution: it stores the "old" Unicode codepoints in 16 bit (i.e. exactly one char) and all the new ones in 32 bit (i.e. two char values). Those two 16 bit values are called a "surrogate pair". Now strictly speaking a char holds a "UTF-16 code unit" instead of "a Unicode character" as it used to.

Now all the "old" methods (handling only char) could be used just fine as long as you didn't use any of the "new" Unicode characters (or didn't really care about them), but if you cared about the new characters as well (or simply need to have complete Unicode support), then you'll need to use the "codepoint" versions that actually support all possible Unicode codepoints.

Note: A very well known example of unicode characters that are not in the BMP (i.e. work only when using the codepoint variant) are Emojis: Even the simple Grinning Face 😀 U+1F600 can't be represented in a single char.

like image 99
Joachim Sauer Avatar answered Oct 02 '22 00:10

Joachim Sauer