あなたは本当に中国語と英語の文字を知っていますか--str.length()とstr.getBytes()です.lengthの違い
2384 ワード
, , “ ” 。
, trunk , , str.length() , , , 。( trunk : xml chunked )
, , , , , , , , ( ), , , !
:
package test;
import java.io.UnsupportedEncodingException;
public class Test {
public static void main(String[] args) {
String str = " , ";
System.out.println(str);
System.out.println("str.length:" + str.length());
System.out.println("str.getBytes().length:" + str.getBytes().length);
try {
System.out.println("str.getBytes('utf-8').length:" + str.getBytes("utf-8").length);
System.out.println("str.getBytes('GBK').length:" +str.getBytes("GBK").length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("--------------------------------------");
String str1 = "I give the test, English";
System.out.println(str1);
System.out.println("str1.length:" + str1.length());
System.out.println("str1.getBytes().length:" + str1.getBytes().length);
try {
System.out.println("str1.getBytes('utf-8').length:" + str1.getBytes("utf-8").length);
System.out.println("str1.getBytes('GBK').length:" +str1.getBytes("GBK").length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
1つ の :
テストをします. str.length:9
str.getBytes().length:27
str.getBytes('utf-8').length:27
str.getBytes('GBK').length:18
--------------------------------------
I give the test, English
str1.length:24
str1.getBytes().length:24
str1.getBytes('utf-8').length:24
str1.getBytes('GBK').length:24
2つ の :
str.length:9 str.getBytes()のテストをします.length:18 str.getBytes('utf-8').length:27 str.getBytes('GBK').length:18 -------------------------------------- I give the test, English str1.length:24 str1.getBytes().length:24 str1.getBytes('utf-8').length:24 str1.getBytes('GBK').length:24