Java Traversal string correctly

Source: Internet
Author: User

A Java string is a series of Unicode characters, but it is often mistaken for a char sequence. So, we often walk through strings like this:

Package Testchar;public class TestChar2 {public static void main (string[] args) {String s = "\u0041\u00df\u6771\ud801\udc0 0 "; for (int i = 0; i < s.length (); i++) {System.out.println (S.charat (i));}}
then, the unexpected results were obtained:





This is because Unicode characters cannot be equated with the char type of java. In fact, the char type in Java can represent a subset of the Unicode characters, because char is only 16 bits, that is, it can only represent 65536 (2 of 16) characters, but the actual number of Unicode characters exceeds this number. In Java, characters in char and string are encoded with UTF-16, and the encoded value corresponding to a character is called a code point. Some code points are encoded with 16-bit encoding, called a unit of code, such as char, and some code points are encoded in 32-bit code, that is, encoded in two contiguous codes, such as \UD801\UDC00. In fact, we traverse a string that iterates through all the code points in the string, and

S.length ()

Returns the number of code units in the string s. When I corresponds to a code unit that is only part of a 32-bit code point,

S.charat (i)

will not work as we would like.

There are several ways to correctly traverse a string:

Package testchar;/** * correctly traverse String * * @author Yuncong * */public class Testchar {public static void main (string[] args) {String s = "\u0041\u00df\u6771\ud801\udc00";//Gets the number of code points in the string int cpcount = S.codepointcount (0, S.length ()); for (int i = 0; i < Cpcount; i++) {int index = s.offsetbycodepoints (0, i); int cp = S.CODEPOINTAT (index); Character.issupplementarycodepoint (CP)) {System.out.println ((char) CP);} else {System.out.println (CP);}} System.out.println ("-------------------"); for (int i = 0; i < s.length (); i++) {int cp = S.CODEPOINTAT (i); if (! Character.issupplementarycodepoint (CP)) {System.out.println ((char) CP);} else {System.out.println (CP); i++;}} System.out.println ("-------------------");//reverse traverse string for (int i = S.length ()-1; I >= 0; i--) {int cp = 0;//when I equals 0, only The next code unit, cannot be the auxiliary character if (i = = 0) {cp = S.codepointat (0); System.out.println ((char) CP);} else {//can only be returned if I is greater than 0, and//Because the remaining code unit is greater than 2, the two code units that are accessed//are likely to indicate a secondary//character;//return a code unit I--;CP = S.codepointat (i); Character.issupplemEntarycodepoint (CP)) {SYSTEM.OUT.PRINTLN (CP);} else {//If the CP is not a secondary character, go back to the normal position of the traverse I++;CP = S.codepointat (i); System.out.println ((char) CP);}}}}

(Sinkhole, no auxiliary characters in Java can appear in the blog.)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Traversal string correctly

Related Article

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: 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.