1.1. 使用String類
1.1.1. 創建String的對象
表示“快學大數據”的字節數據:
[-65, -20, -47, -89, -76, -13, -54, -3, -66, -35]
public class Demo2 {
public static void main(String[] args) throws UnsupportedEncodingException {
test7();
}
private static void test7() {
//演示將一個int數組轉換為字符串對象;(int數組中每一個數組都表示一個字符的編碼)
int[] arr = new int[] {30000,30001,30002};
System.out.println(new String(arr,0,arr.length));//田由甲
}
private static void test6() {
//演示將一個字符數組的一部分轉換為字符串對象
char[] chs = new char[] {'快','學','大','數','據'};
String str = new String(chs,1,2);
System.out.println(str);//學大
}
private static void test5() {
//演示將一個字符數組轉換為字符串
char[] chs = new char[] {'快','學','大','數','據'};
String str = new String(chs);
System.out.println(str);//快學大數據
}
private static void test4() throws UnsupportedEncodingException {
//演示將一個字節數組的一部分,根據指定編碼表轉換為字符串
byte[] byts = new byte[] {-65, -20, -47, -89, -76, -13, -54, -3, -66, -35};
String str = new String(byts,4,6,"GBK");
System.out.println(str);
}
private static void test3() {
//演示將一個字節數組的一部分轉換為字符串對象
byte[] byts = new byte[] {97,98,99,100,101,102};
String str = new String(byts,3,2);
System.out.println(str);//de
}
private static void test2() throws UnsupportedEncodingException {
byte[] byts = new byte[] {-65, -20, -47, -89};
String str = new String(byts,"GBK");
System.out.println(str);
}
private static void test1() {
//演示將一個字節數組轉換為字符串對象
byte[] byts = new byte[] {97,98,99,100,101,102};
String str = new String(byts);
System.out.println(str);
}
}
閱讀更多 IT極客學社 的文章