作者:臧圩人(zangweiren)
網址:http://zangweiren.javaeye.com
>>>轉載請註明出處!<<<
上一次我們已經一起回顧了面試題中常考的到底建立了幾個String對象的相關知識,這一次我們以幾個常見面試題為引子,來回顧一下String對象相關的其它一些方面。
一、String類有length()方法嗎?數組有length()方法嗎?
String類當然有length()方法了,看看String類的源碼就知道了,這是這個方法的定義:
Java代碼
public int length() {
return count;
}
String的長度實際上就是它的屬性--char型數組value的長度。數組是沒有length()方法的,大家知道,在JAVA中,數組也被作為對
象來處理,它的方法都繼承自Object類。數組有一個屬性length,這也是它唯一的屬性,對於所有類型的數組都是這樣。
二、一個中文漢字能儲存在一個char裡嗎?
請看下面的例子:
Java代碼
public class ChineseTest {
public static void main(String[] args) {
// 將一個中文漢字賦值給一個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);
}
}
編譯沒有報錯,運行結果:
1、中文測試成功
答案就不用說了。為什麼一個中文漢字可以儲存在一個char變數裡呢?因為在JAVA中,一個char是2個位元組(byte),而一個中文漢字是一個字元,也是2個位元組。而英文字母都是一個位元組的,因此它也能儲存到一個byte裡,一個中文漢字卻不能。請看:
Java代碼
public class ChineseTest {
public static void main(String[] args) {
// 將一個英文字母賦值給一個byte變數
byte a = 'a';
// 將一個中文漢字賦值給一個byte變數時,編譯會報錯
// byte b = '中';
System.out.println("byte a = " + a);
// System.out.println("byte b = "+b);
}
}
運行結果:
1、byte a = 97
正如大家所看到的那樣,我們實際上是把字元'a'對應的ASCII碼值賦值給了byte型變數a。
讓我們回過頭來看看最初的例子,能不能將a、b、c、d、e、f拼接在一起一次輸出呢?讓我們試試看:
Java代碼
public class ChineseTest {
public static void main(String[] args) {
// 將一個中文漢字賦值給一個char變數
char a = '中';
char b = '文';
char c = '測';
char d = '試';
char e = '成';
char f = '功';
System.out.print(a + b + c + d + e + f);
}
}
運行結果:
1、156035
這顯然不是我們想要的結果。只所以會這樣是因為我們誤用了“+”運算子,當它被用於字串和字串之間,或者字串和其他類型變數之間時,它產生的效果是
字串的拼接;但當它被用於字元和字元之間時,效果等同於用於數字和數字之間,是一種算術運算。因此我們得到的“156035”是'中'、'文'、'測
'、'試'、'成'、'功'這六個漢字分別對應的數值算術相加後的結果。
三、字串的反轉輸出。
這也是面試題中常考的一道。我們就以一個包含了全部26個英文字母,同時又具有完整含義的最短句子作為例子來完成解答。先來看一下這個句子:
引用
A quick brown fox jumps over the lazy dog.(一隻輕巧的棕色狐狸從那條懶狗身上跳了過去。)
最常用的方式就是反向取出每個位置的字元,然後依次將它們輸出到控制台:
Java代碼
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("反轉後字串:");
for (int i = s.length(); i > 0; i--) {
System.out.print(s.charAt(i - 1));
}
// 也可以轉換成數組後再反轉,不過有點多此一舉
char[] data = s.toCharArray();
System.out.println();
System.out.print("反轉後字串:");
for (int i = data.length; i > 0; i--) {
System.out.print(data[i - 1]);
}
}
}
運行結果:
1、原始的字串:A quick brown fox jumps over the lazy dog.
2、反轉後字串:.god yzal eht revo spmuj xof nworb kciuq A
3、反轉後字串:.god yzal eht revo spmuj xof nworb kciuq A
以上兩種方式雖然常用,但卻不是最簡單的方式,更簡單的是使用現有的方法:
Java代碼
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("反轉後字串:");
StringBuffer buff = new StringBuffer(s);
// java.lang.StringBuffer類的reverse()方法可以將字串反轉
System.out.println(buff.reverse().toString());
}
}
運行結果:
1、原始的字串:A quick brown fox jumps over the lazy dog.
2、反轉後字串:.god yzal eht revo spmuj xof nworb kciuq A
四、按位元組截取含有中文漢字的字串。
要求實現一個按位元組截取字串的方法,比如對於字串"我ZWR愛JAVA",截取它的前四位位元組應該是"我ZW",而不是"我ZWR",同時要保證不會出現截取了半個漢字的情況。
英文字母和中文漢字在不同的編碼格式下,所佔用的位元組數也是不同的,我們可以通過下面的例子來看看在一些常見的編碼格式下,一個英文字母和一個中文漢字分別佔用多少位元組。
Java代碼
import java.io.UnsupportedEncodingException;
public class EncodeTest {
/**
* 列印字串在指定編碼下的位元組數和編碼名稱到控制台
*
* @param s
* 字串
* @param encodingName
* 編碼格式
*/
public static void printByteLength(String s, String encodingName) {
System.out.print("位元組數:");
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 = "人";
// 計算一個英文字母在各種編碼下的位元組數
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();
// 計算一個中文漢字在各種編碼下的位元組數
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");
}
}
運行結果如下:
1、英文字母:A
2、位元組數:1;編碼:GB2312
3、位元組數:1;編碼:GBK
4、位元組數:1;編碼:GB18030
5、位元組數:1;編碼:ISO-8859-1
6、位元組數:1;編碼:UTF-8
7、位元組數:4;編碼:UTF-16
8、位元組數:2;編碼:UTF-16BE
9、位元組數:2;編碼:UTF-16LE
10、
11、中文漢字:人
12、位元組數:2;編碼:GB2312
13、位元組數:2;編碼:GBK
14、位元組數:2;編碼:GB18030
15、位元組數:1;編碼:ISO-8859-1
16、位元組數:3;編碼:UTF-8
17、位元組數:4;編碼:UTF-16
18、位元組數:2;編碼:UTF-16BE
19、位元組數:2;編碼:UTF-16LE
UTF-16BE和UTF-16LE是UNICODE編碼家族的兩個成員。UNICODE標準定義了UTF-8、UTF-16、UTF-32三種編碼格
式,共有UTF-8、UTF-16、UTF-16BE、UTF-16LE、UTF-32、UTF-32BE、UTF-32LE七種編碼方案。JAVA所采
用的編碼方案是UTF-16BE。從上例的運行結果中我們可以看出,GB2312、GBK、GB18030三種編碼格式都可以滿足題目的要求。下面我們就
以GBK編碼為例來進行解答。
我們不能直接使用String類的substring(int beginIndex, int
endIndex)方法,因為它是按字元截取的。'我'和'Z'都被作為一個字元來看待,length都是1。實際上我們只要能區分開中文漢字和英文字
母,這個問題就迎刃而解了,而它們的區別就是,中文漢字是兩個位元組,英文字母是一個位元組。
Java代碼
import java.io.UnsupportedEncodingException;
public class CutString {
/**
* 判斷是否是一個中文漢字
*
* @param c
* 字元
* @return true表示是中文漢字,false表示是英文字母
* @throws UnsupportedEncodingException
* 使用了JAVA不支援的編碼格式
*/
public static boolean isChineseChar(char c)
throws UnsupportedEncodingException {
// 如果位元組數大於1,是漢字
// 以這種方式區別英文字母和中文漢字並不是十分嚴謹,但在這個題目中,這樣判斷已經足夠了
return String.valueOf(c).getBytes("GBK").length > 1;
}
/**
* 按位元組截取字串
*
* @param orignal
* 原始字串
* @param count
* 截取位元
* @return 截取後的字串
* @throws UnsupportedEncodingException
* 使用了JAVA不支援的編碼格式
*/
public static String substring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字元不為null,也不是Null 字元串
if (orignal != null && !"".equals(orignal)) {
// 將原始字串轉換為GBK編碼格式
orignal = new String(orignal.getBytes(), "GBK");
// 要截取的位元組數大於0,且小於原始字串的位元組數
if (count > 0 && count < orignal.getBytes("GBK").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 遇到中文漢字,截取位元組總數減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();
}
}
}
運行結果:
1、原始字串:我ZWR愛JAVA
2、截取前1位:我
3、截取前2位:我
4、截取前4位:我ZW
5、截取前6位:我ZWR愛