String Character Length: Difference between revisions
Content deleted Content added
Line 171: | Line 171: | ||
'''Compiler:''' any Java compiler should do |
'''Compiler:''' any Java compiler should do |
||
Java encodes strings in UTF-16, which represents each character with one or two 16-bit values. The most commonly used characters are represented by one 16-bit value, while rarer ones like some mathematical symbols are represented by two. |
|||
The length method of String objects gives the number of 16-bit values used to encode a string. |
|||
String s = "Hello, world!"; |
String s = "Hello, world!"; |
||
int length = s.length(); |
int length = s.length(); |
||
Since Java 1.5, the actual number of characters can be determined by calling the codePointCount method. |
|||
int length = str.codePointCount(0, str.length); |
|||
==[[JavaScript]]== |
==[[JavaScript]]== |