Java字符串中常用字符占用字節數
阿新 • • 發佈:2018-09-28
con col get 英文 font bottom margin over left
java中一個char型的數據(也就是一個字符)占兩個字節。而Java中常用的字符包括數字、英文字母、英文符號、中文漢字、中文符號等,若在字符串中包含裏面的多種字符,它們是否都占兩個字符呢?答案是否定的。
public class CharBytes {public static void main(String[] args) {String s1 = "1234567";// 7個數字字符byte[] b1 = s1.getBytes();
System.out.println("7個數字字符1234567所占的字節數為:" + b1.length);
String s2 = "abcdefg";// 7個英文字符byte[] b2 = s2.getBytes();
System.out.println("7個英文字符abcdefg所占的字節數為:" + b2.length);
String s3 = "::<>{}?";// 7個英文符號字符byte[] b3 = s3.getBytes();
System.out.println("7個英文符號字符::<>{}?所占的字節數為:" + b3.length);
String s4 = "釣魚島是中國的";// 7個中文漢字字符byte[] b4 = s4.getBytes();
System.out.println("釣魚島是中國的所占的字節數為:" + b4.length);
String s5 = "【】《》?:!";// 7個中文符號字符byte[] b5 = s5.getBytes();
System.out.println("7個中文符號字符 【】《》?:! 所占的字節數為:" + b5.length);
String s6 = "/n";
byte[] b6 = s6.getBytes();
System.out.println("/n所占的字節數為:" + b6.length);
}}
運行結果為:
7個數字字符1234567所占的字節數為:77個英文字符abcdefg所占的字節數為:77個英文符號字符::<>{}?所占的字節數為:7釣魚島是中國的所占的字節數為:147個中文符號字符 【】《》?:! 所占的字節數為:14/n所占的字節數為:2
Java字符串中常用字符占用字節數