Coding problem of String inside Java

Source: Internet
Author: User

Java inside the built-in strings are all UTF-16 encoding, detailed coding way to see here

import Java.nio.charset.charset;import java.util.arrays;import java.util.Map; Public classString2bytes { Public Static voidMain (string[] args) {String str="\u005bab Me"; Map<string, charset> charsetmap =charset.availablecharsets ();  for(String charsetName:charsetMap.keySet ()) {System. out. println (CharsetName +":"+ Charsetmap.Get(CharsetName)); } System. out. println (Str.charat (3)); //The GetBytes () method of the string is to get a string of byte arrays, which is well known. //However, it is important to note that this method returns a byte array of the operating system's default encoding format. //If you do not take this into account when using this method, you will find that running a good system on one platform can cause unexpected problems when placed on another machine. System. out. println (Arrays.tostring (Str.getbytes ())); //Additional-level characters        Char[] C = Character.tochars (Integer.parseint ("1d306", -)); String str1=NewString (c); System. out. println (STR1); //Additional-level characters occupy 2 code unitsSystem. out. println (C.length); System. out. println (Str1.length ()); //but it only takes one code pointSystem. out. println (Str1.codepointcount (0, Str1.length ())); //to access the first unit of code, this is a bad practiceSystem. out. println (Str1.charat (0)); //See how it's stored in memory        /** algorithm * H = Math.floor ((c-0x10000)/0x400) +0xd800 * L = (c-0x10000)% 0x400 + 0xdc00 */System. out. println (Integer.tostring (int) Str1.charat (0), -));//0xd834System. out. println (Integer.tostring (int) Str1.charat (1), -));//0xDF06//access the first code point, the right approach        intindex = str1.offsetbycodepoints (0,0); System. out. println (Str1.codepointat (index)); String str2= str1 +"ABC"; //sequential access to a string in the correct way        intCount = Str2.codepointcount (0, Str2.length ());  for(intI=0; I < count; ++i) {intIndex1 = Str2.offsetbycodepoints (0, i); intCP =Str2.codepointat (INDEX1); System. out. Print (Character.tochars (CP)); } System. out. println (); }}

Coding problem of String inside Java

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