好東西分享
上一次我們已經(jīng)一起回顧了面試題中常考的到底創(chuàng)建了幾個(gè)String對象的相關(guān)知識,這一次我們以幾個(gè)常見面試題為引子,來回顧一下String對象相關(guān)的其它一些方面。
String的length()方法和數(shù)組的length屬性
String類有l(wèi)ength()方法嗎?數(shù)組有l(wèi)ength()方法嗎?
String類當(dāng)然有l(wèi)ength()方法了,看看String類的源碼就知道了,這是這個(gè)方法的定義:
- public ? int ?length()?{ ??
- ???? return ?count; ??
- }??
public int length() {
return count;
}
String的長度實(shí)際上就是它的屬性--char型數(shù)組value的長度。數(shù)組是沒有l(wèi)ength()方法的,大家知道,在JAVA中,數(shù)組也被作為對象來處理,它的方法都繼承自O(shè)bject類。數(shù)組有一個(gè)屬性length,這也是它唯一的屬性,對于所有類型的數(shù)組都是這樣。
中文漢字在char中的保存
一個(gè)中文漢字能保存在一個(gè)char類型里嗎?
請看下面的例子:
- public ? class ?ChineseTest?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?將一個(gè)中文漢字賦值給一個(gè)char變量 ??
- ???????? char ?a?=? '中' ; ??
- ???????? char ?b?=? '文' ; ??
- ???????? char ?c?=? '測' ; ??
- ???????? char ?d?=? '試' ; ??
- ???????? char ?e?=? '成' ; ??
- ???????? char ?f?=? '功' ; ??
- ????????System.out.print(a); ??
- ????????System.out.print(b); ??
- ????????System.out.print(c); ??
- ????????System.out.print(d); ??
- ????????System.out.print(e); ??
- ????????System.out.print(f); ??
- ????} ??
- }??
public class ChineseTest {
public static void main(String[] args) {
// 將一個(gè)中文漢字賦值給一個(gè)char變量
char a = '中';
char b = '文';
char c = '測';
char d = '試';
char e = '成';
char f = '功';
System.out.print(a);
System.out.print(b);
System.out.print(c);
System.out.print(d);
System.out.print(e);
System.out.print(f);
}
}
編譯沒有報(bào)錯(cuò),運(yùn)行結(jié)果:
- 中文測試成功
答案就不用說了。為什么一個(gè)中文漢字可以保存在一個(gè)char變量里呢?因?yàn)樵贘AVA中,一個(gè)char是2個(gè)字節(jié)(byte),而一個(gè)中文漢字是一個(gè)字符,也是2個(gè)字節(jié)。而英文字母都是一個(gè)字節(jié)的,因此它也能保存到一個(gè)byte里,一個(gè)中文漢字卻不能。請看:
- public ? class ?ChineseTest?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?將一個(gè)英文字母賦值給一個(gè)byte變量 ??
- ???????? byte ?a?=? 'a' ; ??
- ???????? //?將一個(gè)中文漢字賦值給一個(gè)byte變量時(shí),編譯會(huì)報(bào)錯(cuò) ??
- ???????? //?byte?b?=?'中'; ??
- ??
- ????????System.out.println( "byte?a?=?" ?+?a); ??
- ???????? //?System.out.println("byte?b?=?"+b); ??
- ????} ??
- }??
public class ChineseTest {
public static void main(String[] args) {
// 將一個(gè)英文字母賦值給一個(gè)byte變量
byte a = 'a';
// 將一個(gè)中文漢字賦值給一個(gè)byte變量時(shí),編譯會(huì)報(bào)錯(cuò)
// byte b = '中';
System.out.println("byte a = " + a);
// System.out.println("byte b = "+b);
}
}
運(yùn)行結(jié)果:
- byte a = 97
正如大家所看到的那樣,我們實(shí)際上是把字符'a'對應(yīng)的ASCII碼值賦值給了byte型變量a。
讓我們回過頭來看看最初的例子,能不能將a、b、c、d、e、f拼接在一起一次輸出呢?讓我們試試看:
- public ? class ?ChineseTest?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?將一個(gè)中文漢字賦值給一個(gè)char變量 ??
- ???????? char ?a?=? '中' ; ??
- ???????? char ?b?=? '文' ; ??
- ???????? char ?c?=? '測' ; ??
- ???????? char ?d?=? '試' ; ??
- ???????? char ?e?=? '成' ; ??
- ???????? char ?f?=? '功' ; ??
- ????????System.out.print(a?+?b?+?c?+?d?+?e?+?f); ??
- ????} ??
- }??
public class ChineseTest {
public static void main(String[] args) {
// 將一個(gè)中文漢字賦值給一個(gè)char變量
char a = '中';
char b = '文';
char c = '測';
char d = '試';
char e = '成';
char f = '功';
System.out.print(a + b + c + d + e + f);
}
}
運(yùn)行結(jié)果:
- 156035
這顯然不是我們想要的結(jié)果。只所以會(huì)這樣是因?yàn)槲覀冋`用了“+”運(yùn)算符,當(dāng)它被用于字符串和字符串之間,或者字符串和其他類型變量之間時(shí),它產(chǎn)生的效果是字符串的拼接;但當(dāng)它被用于字符和字符之間時(shí),效果等同于用于數(shù)字和數(shù)字之間,是一種算術(shù)運(yùn)算。因此我們得到的“156035”是'中'、'文'、'測'、'試'、'成'、'功'這六個(gè)漢字分別對應(yīng)的數(shù)值算術(shù)相加后的結(jié)果。
字符串的反轉(zhuǎn)輸出
這也是面試題中常考的一道。我們就以一個(gè)包含了全部26個(gè)英文字母,同時(shí)又具有完整含義的最短句子作為例子來完成解答。先來看一下這個(gè)句子:
最常用的方式就是反向取出每個(gè)位置的字符,然后依次將它們輸出到控制臺(tái):
- public ? class ?StringReverse?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?原始字符串 ??
- ????????String?s?=? "A?quick?brown?fox?jumps?over?the?lazy?dog." ; ??
- ????????System.out.println( "原始的字符串:" ?+?s); ??
- ??
- ????????System.out.print( "反轉(zhuǎn)后字符串:" ); ??
- ???????? for ?( int ?i?=?s.length();?i?>? 0 ;?i--)?{ ??
- ????????????System.out.print(s.charAt(i?-? 1 )); ??
- ????????} ??
- ??
- ???????? //?也可以轉(zhuǎn)換成數(shù)組后再反轉(zhuǎn),不過有點(diǎn)多此一舉 ??
- ???????? char []?data?=?s.toCharArray(); ??
- ????????System.out.println(); ??
- ????????System.out.print( "反轉(zhuǎn)后字符串:" ); ??
- ???????? for ?( int ?i?=?data.length;?i?>? 0 ;?i--)?{ ??
- ????????????System.out.print(data[i?-? 1 ]); ??
- ????????} ??
- ????} ??
- }??
public class StringReverse {
public static void main(String[] args) {
// 原始字符串
String s = "A quick brown fox jumps over the lazy dog.";
System.out.println("原始的字符串:" + s);
System.out.print("反轉(zhuǎn)后字符串:");
for (int i = s.length(); i > 0; i--) {
System.out.print(s.charAt(i - 1));
}
// 也可以轉(zhuǎn)換成數(shù)組后再反轉(zhuǎn),不過有點(diǎn)多此一舉
char[] data = s.toCharArray();
System.out.println();
System.out.print("反轉(zhuǎn)后字符串:");
for (int i = data.length; i > 0; i--) {
System.out.print(data[i - 1]);
}
}
}
運(yùn)行結(jié)果:
- 原始的字符串:A quick brown fox jumps over the lazy dog.
- 反轉(zhuǎn)后字符串:.god yzal eht revo spmuj xof nworb kciuq A
- 反轉(zhuǎn)后字符串:.god yzal eht revo spmuj xof nworb kciuq A
以上兩種方式雖然常用,但卻不是最簡單的方式,更簡單的是使用現(xiàn)有的方法:
- public ? class ?StringReverse?{ ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?原始字符串 ??
- ????????String?s?=? "A?quick?brown?fox?jumps?over?the?lazy?dog." ; ??
- ????????System.out.println( "原始的字符串:" ?+?s); ??
- ??
- ????????System.out.print( "反轉(zhuǎn)后字符串:" ); ??
- ????????StringBuffer?buff?=? new ?StringBuffer(s); ??
- ???????? //?java.lang.StringBuffer類的reverse()方法可以將字符串反轉(zhuǎn) ??
- ????????System.out.println(buff.reverse().toString()); ??
- ????} ??
- }??
public class StringReverse {
public static void main(String[] args) {
// 原始字符串
String s = "A quick brown fox jumps over the lazy dog.";
System.out.println("原始的字符串:" + s);
System.out.print("反轉(zhuǎn)后字符串:");
StringBuffer buff = new StringBuffer(s);
// java.lang.StringBuffer類的reverse()方法可以將字符串反轉(zhuǎn)
System.out.println(buff.reverse().toString());
}
}
運(yùn)行結(jié)果:
- 原始的字符串:A quick brown fox jumps over the lazy dog.
- 反轉(zhuǎn)后字符串:.god yzal eht revo spmuj xof nworb kciuq A
按字節(jié)截取含有中文漢字的字符串
要求實(shí)現(xiàn)一個(gè)按字節(jié)截取字符串的方法,比如對于字符串"我ZWR愛JAVA",截取它的前四位字節(jié)應(yīng)該是"我ZW",而不是"我ZWR",同時(shí)要保證不會(huì)出現(xiàn)截取了半個(gè)漢字的情況。
英文字母和中文漢字在不同的編碼格式下,所占用的字節(jié)數(shù)也是不同的,我們可以通過下面的例子來看看在一些常見的編碼格式下,一個(gè)英文字母和一個(gè)中文漢字分別占用多少字節(jié)。
- import ?java.io.UnsupportedEncodingException; ??
- ??
- public ? class ?EncodeTest?{ ??
- ???? /** ?
- ?????*?打印字符串在指定編碼下的字節(jié)數(shù)和編碼名稱到控制臺(tái) ?
- ?????*? ?
- ?????*?@param?s ?
- ?????*????????????字符串 ?
- ?????*?@param?encodingName ?
- ?????*????????????編碼格式 ?
- ?????*/ ??
- ???? public ? static ? void ?printByteLength(String?s,?String?encodingName)?{ ??
- ????????System.out.print( "字節(jié)數(shù):" ); ??
- ???????? try ?{ ??
- ????????????System.out.print(s.getBytes(encodingName).length); ??
- ????????}? catch ?(UnsupportedEncodingException?e)?{ ??
- ????????????e.printStackTrace(); ??
- ????????} ??
- ????????System.out.println( ";編碼:" ?+?encodingName); ??
- ????} ??
- ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ????????String?en?=? "A" ; ??
- ????????String?ch?=? "人" ; ??
- ??
- ???????? //?計(jì)算一個(gè)英文字母在各種編碼下的字節(jié)數(shù) ??
- ????????System.out.println( "英文字母:" ?+?en); ??
- ????????EncodeTest.printByteLength(en,? "GB2312" ); ??
- ????????EncodeTest.printByteLength(en,? "GBK" ); ??
- ????????EncodeTest.printByteLength(en,? "GB18030" ); ??
- ????????EncodeTest.printByteLength(en,? "ISO-8859-1" ); ??
- ????????EncodeTest.printByteLength(en,? "UTF-8" ); ??
- ????????EncodeTest.printByteLength(en,? "UTF-16" ); ??
- ????????EncodeTest.printByteLength(en,? "UTF-16BE" ); ??
- ????????EncodeTest.printByteLength(en,? "UTF-16LE" ); ??
- ??
- ????????System.out.println(); ??
- ??
- ???????? //?計(jì)算一個(gè)中文漢字在各種編碼下的字節(jié)數(shù) ??
- ????????System.out.println( "中文漢字:" ?+?ch); ??
- ????????EncodeTest.printByteLength(ch,? "GB2312" ); ??
- ????????EncodeTest.printByteLength(ch,? "GBK" ); ??
- ????????EncodeTest.printByteLength(ch,? "GB18030" ); ??
- ????????EncodeTest.printByteLength(ch,? "ISO-8859-1" ); ??
- ????????EncodeTest.printByteLength(ch,? "UTF-8" ); ??
- ????????EncodeTest.printByteLength(ch,? "UTF-16" ); ??
- ????????EncodeTest.printByteLength(ch,? "UTF-16BE" ); ??
- ????????EncodeTest.printByteLength(ch,? "UTF-16LE" ); ??
- ????} ??
- }??
import java.io.UnsupportedEncodingException;
public class EncodeTest {
/**
* 打印字符串在指定編碼下的字節(jié)數(shù)和編碼名稱到控制臺(tái)
*
* @param s
* 字符串
* @param encodingName
* 編碼格式
*/
public static void printByteLength(String s, String encodingName) {
System.out.print("字節(jié)數(shù):");
try {
System.out.print(s.getBytes(encodingName).length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(";編碼:" + encodingName);
}
public static void main(String[] args) {
String en = "A";
String ch = "人";
// 計(jì)算一個(gè)英文字母在各種編碼下的字節(jié)數(shù)
System.out.println("英文字母:" + en);
EncodeTest.printByteLength(en, "GB2312");
EncodeTest.printByteLength(en, "GBK");
EncodeTest.printByteLength(en, "GB18030");
EncodeTest.printByteLength(en, "ISO-8859-1");
EncodeTest.printByteLength(en, "UTF-8");
EncodeTest.printByteLength(en, "UTF-16");
EncodeTest.printByteLength(en, "UTF-16BE");
EncodeTest.printByteLength(en, "UTF-16LE");
System.out.println();
// 計(jì)算一個(gè)中文漢字在各種編碼下的字節(jié)數(shù)
System.out.println("中文漢字:" + ch);
EncodeTest.printByteLength(ch, "GB2312");
EncodeTest.printByteLength(ch, "GBK");
EncodeTest.printByteLength(ch, "GB18030");
EncodeTest.printByteLength(ch, "ISO-8859-1");
EncodeTest.printByteLength(ch, "UTF-8");
EncodeTest.printByteLength(ch, "UTF-16");
EncodeTest.printByteLength(ch, "UTF-16BE");
EncodeTest.printByteLength(ch, "UTF-16LE");
}
}
運(yùn)行結(jié)果如下:
- 英文字母:A
- 字節(jié)數(shù):1;編碼:GB2312
- 字節(jié)數(shù):1;編碼:GBK
- 字節(jié)數(shù):1;編碼:GB18030
- 字節(jié)數(shù):1;編碼:ISO-8859-1
- 字節(jié)數(shù):1;編碼:UTF-8
- 字節(jié)數(shù):4;編碼:UTF-16
- 字節(jié)數(shù):2;編碼:UTF-16BE
- 字節(jié)數(shù):2;編碼:UTF-16LE
- 中文漢字:人
- 字節(jié)數(shù):2;編碼:GB2312
- 字節(jié)數(shù):2;編碼:GBK
- 字節(jié)數(shù):2;編碼:GB18030
- 字節(jié)數(shù):1;編碼:ISO-8859-1
- 字節(jié)數(shù):3;編碼:UTF-8
- 字節(jié)數(shù):4;編碼:UTF-16
- 字節(jié)數(shù):2;編碼:UTF-16BE
- 字節(jié)數(shù):2;編碼:UTF-16LE
UTF-16BE和UTF-16LE是UNICODE編碼家族的兩個(gè)成員。UNICODE標(biāo)準(zhǔn)定義了UTF-8、UTF-16、UTF-32三種編碼格式,共有UTF-8、UTF-16、UTF-16BE、UTF-16LE、UTF-32、UTF-32BE、UTF-32LE七種編碼方案。JAVA所采用的編碼方案是UTF-16BE。從上例的運(yùn)行結(jié)果中我們可以看出,GB2312、GBK、GB18030三種編碼格式都可以滿足題目的要求。下面我們就以GBK編碼為例來進(jìn)行解答。
如果我們直接按照字節(jié)截取會(huì)出現(xiàn)什么情況呢?我們來測試一下:
- import ?java.io.UnsupportedEncodingException;??? ??
- ?? ??
- public ? class ?CutString?{??? ??
- ???? public ? static ? void ?main(String[]?args)? throws ?UnsupportedEncodingException?{??? ??
- ????????String?s?=? "我ZWR愛JAVA" ;??? ??
- ???????? //?獲取GBK編碼下的字節(jié)數(shù)據(jù)??? ??
- ???????? byte []?data?=?s.getBytes( "GBK" );??? ??
- ???????? byte []?tmp?=? new ? byte [ 6 ];??? ??
- ???????? //?將data數(shù)組的前六個(gè)字節(jié)拷貝到tmp數(shù)組中??? ??
- ????????System.arraycopy(data,? 0 ,?tmp,? 0 ,? 6 );??? ??
- ???????? //?將截取到的前六個(gè)字節(jié)以字符串形式輸出到控制臺(tái)??? ??
- ????????s?=? new ?String(tmp);??? ??
- ????????System.out.println(s);??? ??
- ????}??? ??
- }???
import java.io.UnsupportedEncodingException;
public class CutString {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "我ZWR愛JAVA";
// 獲取GBK編碼下的字節(jié)數(shù)據(jù)
byte[] data = s.getBytes("GBK");
byte[] tmp = new byte[6];
// 將data數(shù)組的前六個(gè)字節(jié)拷貝到tmp數(shù)組中
System.arraycopy(data, 0, tmp, 0, 6);
// 將截取到的前六個(gè)字節(jié)以字符串形式輸出到控制臺(tái)
s = new String(tmp);
System.out.println(s);
}
}
輸出結(jié)果:
- 我ZWR?
在截取前六個(gè)字節(jié)時(shí),第二個(gè)漢字“愛”被截取了一半,導(dǎo)致它無法正常顯示了,這樣顯然是有問題的。
我們不能直接使用String類的substring(int beginIndex, int endIndex)方法,因?yàn)樗前醋址厝〉摹?我'和'Z'都被作為一個(gè)字符來看待,length都是1。實(shí)際上我們只要能區(qū)分開中文漢字和英文字母,這個(gè)問題就迎刃而解了,而它們的區(qū)別就是,中文漢字是兩個(gè)字節(jié),英文字母是一個(gè)字節(jié)。
- import ?java.io.UnsupportedEncodingException; ??
- ??
- public ? class ?CutString?{ ??
- ??
- ???? /** ?
- ?????*?判斷是否是一個(gè)中文漢字 ?
- ?????*? ?
- ?????*?@param?c ?
- ?????*????????????字符 ?
- ?????*?@return?true表示是中文漢字,false表示是英文字母 ?
- ?????*?@throws?UnsupportedEncodingException ?
- ?????*?????????????使用了JAVA不支持的編碼格式 ?
- ?????*/ ??
- ???? public ? static ? boolean ?isChineseChar( char ?c) ??
- ???????????? throws ?UnsupportedEncodingException?{ ??
- ???????? //?如果字節(jié)數(shù)大于1,是漢字 ??
- ???????? //?以這種方式區(qū)別英文字母和中文漢字并不是十分嚴(yán)謹(jǐn),但在這個(gè)題目中,這樣判斷已經(jīng)足夠了 ??
- ???????? return ?String.valueOf(c).getBytes( "GBK" ).length?>? 1 ; ??
- ????} ??
- ??
- ???? /** ?
- ?????*?按字節(jié)截取字符串 ?
- ?????*? ?
- ?????*?@param?orignal ?
- ?????*????????????原始字符串 ?
- ?????*?@param?count ?
- ?????*????????????截取位數(shù) ?
- ?????*?@return?截取后的字符串 ?
- ?????*?@throws?UnsupportedEncodingException ?
- ?????*?????????????使用了JAVA不支持的編碼格式 ?
- ?????*/ ??
- ???? public ? static ?String?substring(String?orignal,? int ?count) ??
- ???????????? throws ?UnsupportedEncodingException?{ ??
- ???????? //?原始字符不為null,也不是空字符串 ??
- ???????? if ?(orignal?!=? null ?&&?! "" .equals(orignal))?{ ??
- ???????????? //?將原始字符串轉(zhuǎn)換為GBK編碼格式 ??
- ????????????orignal?=? new ?String(orignal.getBytes(),? "GBK" ); ??
- ???????????? //?要截取的字節(jié)數(shù)大于0,且小于原始字符串的字節(jié)數(shù) ??
- ???????????? if ?(count?>? 0 ?&&?count?<?orignal.getBytes( "GBK" ).length)?{ ??
- ????????????????StringBuffer?buff?=? new ?StringBuffer(); ??
- ???????????????? char ?c; ??
- ???????????????? for ?( int ?i?=? 0 ;?i?<?count;?i++)?{ ??
- ???????????????????? //?charAt(int?index)也是按照字符來分解字符串的 ??
- ????????????????????c?=?orignal.charAt(i); ??
- ????????????????????buff.append(c); ??
- ???????????????????? if ?(CutString.isChineseChar(c))?{ ??
- ???????????????????????? //?遇到中文漢字,截取字節(jié)總數(shù)減1 ??
- ????????????????????????--count; ??
- ????????????????????} ??
- ????????????????} ??
- ???????????????? return ?buff.toString(); ??
- ????????????} ??
- ????????} ??
- ???????? return ?orignal; ??
- ????} ??
- ??
- ???? public ? static ? void ?main(String[]?args)?{ ??
- ???????? //?原始字符串 ??
- ????????String?s?=? "我ZWR愛JAVA" ; ??
- ????????System.out.println( "原始字符串:" ?+?s); ??
- ???????? try ?{ ??
- ????????????System.out.println( "截取前1位:" ?+?CutString.substring(s,? 1 )); ??
- ????????????System.out.println( "截取前2位:" ?+?CutString.substring(s,? 2 )); ??
- ????????????System.out.println( "截取前4位:" ?+?CutString.substring(s,? 4 )); ??
- ????????????System.out.println( "截取前6位:" ?+?CutString.substring(s,? 6 )); ??
- ????????}? catch ?(UnsupportedEncodingException?e)?{ ??
- ????????????e.printStackTrace(); ??
- ????????} ??
- ????} ??
- }??
import java.io.UnsupportedEncodingException;
public class CutString {
/**
* 判斷是否是一個(gè)中文漢字
*
* @param c
* 字符
* @return true表示是中文漢字,false表示是英文字母
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的編碼格式
*/
public static boolean isChineseChar(char c)
throws UnsupportedEncodingException {
// 如果字節(jié)數(shù)大于1,是漢字
// 以這種方式區(qū)別英文字母和中文漢字并不是十分嚴(yán)謹(jǐn),但在這個(gè)題目中,這樣判斷已經(jīng)足夠了
return String.valueOf(c).getBytes("GBK").length > 1;
}
/**
* 按字節(jié)截取字符串
*
* @param orignal
* 原始字符串
* @param count
* 截取位數(shù)
* @return 截取后的字符串
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的編碼格式
*/
public static String substring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字符不為null,也不是空字符串
if (orignal != null && !"".equals(orignal)) {
// 將原始字符串轉(zhuǎn)換為GBK編碼格式
orignal = new String(orignal.getBytes(), "GBK");
// 要截取的字節(jié)數(shù)大于0,且小于原始字符串的字節(jié)數(shù)
if (count > 0 && count < orignal.getBytes("GBK").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
// charAt(int index)也是按照字符來分解字符串的
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 遇到中文漢字,截取字節(jié)總數(shù)減1
--count;
}
}
return buff.toString();
}
}
return orignal;
}
public static void main(String[] args) {
// 原始字符串
String s = "我ZWR愛JAVA";
System.out.println("原始字符串:" + s);
try {
System.out.println("截取前1位:" + CutString.substring(s, 1));
System.out.println("截取前2位:" + CutString.substring(s, 2));
System.out.println("截取前4位:" + CutString.substring(s, 4));
System.out.println("截取前6位:" + CutString.substring(s, 6));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
- 原始字符串:我ZWR愛JAVA
- 截取前1位:我
- 截取前2位:我
- 截取前4位:我ZW
- 截取前6位:我ZWR愛
向原作者致敬,轉(zhuǎn)自:
作者:臧圩人(zangweiren)
網(wǎng)址:
http://zangweiren.iteye.com
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

