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