Length method for string in Java __java

Source: Internet
Author: User

First of all, char type:
The char type is used to represent a single character. To figure out the char type, you must understand the Unicode encoding table.
Determines the use of a 16-bit Unicode character set when designing Java. can represent 65,536 characters.
It is a pity that after a while, the inevitable happens. There are more than 65,536 Unicode characters.
(The main reason is the addition of a large number of Chinese, Japanese, Korean, such as ideographic), that is, 16-bit
The char type has not been able to meet the needs of all Unicode characters.

Code point: Refers to the code value that corresponds to a character in a coded table.
In the Unicode standard, code points are written in hexadecimal and prefixed with u+, for example, u+0041 is
The code point for the letter A.

The code points for Unicode can be divided into 17 code levels (plane).
The first level of code becomes the basic multilingual level (Multilingual plane),
Code points from the u+0000 large u+ffff, which contains the classic Unicode code.
The remaining 16 additional levels of code points from u+10000 to U+10FFFF, including some auxiliary characters
(supplementary character).

The UTF-16 encoding represents all Unicode code points using different lengths of encoding. At the basic level of multiple languages,
Each character is represented by a 16-bit, usually called a code unit. and the auxiliary character is a pair of consecutive
Code unit for the encoding.

Code points and code units in Java:

The Java string consists of a char sequence. The character data type is a Unicode code point that is represented by a UTF-16 encoding
Unit of code.
Most common Unicode characters use a single unit of code to represent
Auxiliary characters require a pair of code unit representations.

The length method returns the number of code units required for the given string represented by the UTF-16 encoding.
String greeting = "Hello";
int n = greeting.length (); is 5
To get the actual length, that is, the number of code points, you can call:
int cpcount = Greeting.codepointcount (0, Greeting.length ());

Excerpt from Java Core Technology Volume I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.