首页 > 代码库 > gbk utf-8 string java

gbk utf-8 string java

String d = "汗d";
String e = "喊";
String f = "d";
System.out.println("String length is " + d.length() + ". " + d.getBytes("GBK").length
+ " bytes in GBK or " + d.getBytes("UTF-8").length
+ " bytes in UTF-8");

System.out.println("char array length is " + d.toCharArray().length);
System.out.println("String length is " + e.length() + ". " + e.getBytes("GBK").length
+ " bytes in GBK or " + e.getBytes("UTF-8").length
+ " bytes in UTF-8");
System.out.println("char array length is " + e.toCharArray().length);
System.out.println("String length is " +f.length() + ". " + f.getBytes("GBK").length
+ " bytes in GBK or " + f.getBytes("UTF-8").length
+ " bytes in UTF-8");
System.out.println("char array length is " + f.toCharArray().length);

System.out.println("default encoding is " +System.getProperty("file.encoding"));

 

//output

String length is 2. 3 bytes in GBK or 4 bytes in UTF-8
char array length is 2
String length is 1. 2 bytes in GBK or 3 bytes in UTF-8
char array length is 1
String length is 1. 1 bytes in GBK or 1 bytes in UTF-8
char array length is 1
default encoding is UTF-8

//output - 2

 

String length is 2. 3 bytes in GBK or 4 bytes in UTF-8
char array length is 2
String length is 1. 2 bytes in GBK or 3 bytes in UTF-8
char array length is 1
String length is 1. 1 bytes in GBK or 1 bytes in UTF-8
char array length is 1
default encoding is GBK

gbk utf-8 string java