Iterating over Unicode Codepoints in Java Strings
While the String class provides the codePointAt(int) method for accessing Unicode codepoints, its indexing relies on character offsets rather than codepoint offsets. This raises concerns about handling characters within the high-surrogate range and the efficiency of the proposed iteration approach using character-by-character scanning.
Improved Iteration Solution
Java's internal String representation employs a UTF-16-based encoding scheme. Characters outside the Basic Multilingual Plane (BMP) are encoded using the surrogacy scheme. For efficient iteration, consider using the following canonical approach:
final int length = s.length();
for (int offset = 0; offset This approach correctly handles surrogate pairs for characters outside the BMP. By utilizing Character.charCount(codepoint), it efficiently increments the offset by the appropriate number of characters for each codepoint.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3