編碼格式不一致造成的亂碼

package com.qf.demo2;

import java.io.UnsupportedEncodingException;

// ????? 亂碼
public class Test3 {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String string = new  String();// 創(chuàng)建的是 空字符序列
        System.out.println(string);
        
        byte[] bs = new byte[]{97,98,99};// 1,2 
        
         // 編碼格式不同會(huì)造成  亂碼
        //getBytes() 是Java編程語言中將一個(gè)字符串轉(zhuǎn)化為一個(gè)字節(jié)數(shù)組byte[]的方法
        byte[] bs2 = "你好".getBytes();// 當(dāng)前的編碼格式   gbk
        
        String string2 = new String(bs2,"gbk");//此處要與bs2的編碼格式一致 若把gbk改為utf-8 則會(huì)出現(xiàn)亂碼
        System.out.println("sss"+string2);
        
        //offset 偏移量  (從下表哪個(gè)位置開始)  length 長度 要轉(zhuǎn)成的字符串中  需要 數(shù)組中及格數(shù)據(jù)
        // offset + length <= 數(shù)組.length
        String string3 = new String(bs, 1, 2);// StringIndexOutOfBoundsException
        System.out.println(string3);
        
        
        char[] cs = new char[]{'a','b','c'};
        String string4 = new String(cs);
        System.out.println(string4);    
        String string5 = new String(cs, 1, 2);
        System.out.println(string5);
        String string6 = new String("abcdefagdafda");
        System.out.println(string6);
        
        // 字符串緩沖區(qū)
        //String string7 =new String(buffer)
        //String string7 = new String(builder)
        
        // 從字符串中 獲取指定下標(biāo)的  字符
        char c = string6.charAt(2);
        System.out.println(c);
        
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容