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ù)。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
推薦閱讀更多精彩內(nèi)容
- 前提:最近在eclipse中執(zhí)行ant腳本編輯java文件時(shí),遇到字符不識(shí)別問題,問題是因?yàn)轫?xiàng)目編碼同工具編碼不一...
- 【蝴蝶效應(yīng)】 蝴蝶效應(yīng):上個(gè)世紀(jì)70年代,美國一個(gè)名叫洛倫茲的氣象學(xué)家在解釋空氣系統(tǒng)理論時(shí)說,亞馬遜雨林一只蝴蝶...
- 今天晚上我早早的去接孩子了,接到他后告訴他晚上帶他去我姐家吃飯,他聽后高興的答應(yīng)了。 到了姐姐家門口,他自...
- 前段時(shí)間男朋友很少聯(lián)系我,十天才有一個(gè)電話,而且是我主動(dòng)。問理由,他說前兩天不是剛聯(lián)系過了么,,還說我無理取鬧,鬧...