---
title: "JAVA面试题解惑系列（六）——字符串（String）杂谈"
author: "Komes"
date: "2010-05-01T00:04:25Z"
tags: []
categories: ["非技术"]
slug: "java-string"
draft: false
lastmod: "2010-05-01T00:04:25Z"
id: 657373
url: "https://komes.iteye.com/blog/657373"
---

好东西分享  
上一次我们已经一起回顾了面试题中常考的到底创建了几个String对象的相关知识，这一次我们以几个常见面试题为引子，来回顾一下String对象相关的其它一些方面。

***String的length()方法和数组的length属性***

String类有length()方法吗？数组有length()方法吗？

String类当然有length()方法了，看看String类的源码就知道了，这是这个方法的定义：

Java代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

1. **public** **int** length() {
2. **return** count;
3. }

```
public int length() {
    return count;
}
```

String的长度实际上就是它的属性--char型数组value的长度。数组是没有length()方法的，大家知道，在JAVA中，数组也被作为对象来处理，它的方法都继承自Object类。数组有一个属性length，这也是它唯一的属性，对于所有类型的数组都是这样。

***中文汉字在char中的保存***

一个中文汉字能保存在一个char类型里吗？

请看下面的例子：

Java代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **public** **class** ChineseTest {
02. **public** **static** **void** main(String\[] args) {
03. // 将一个中文汉字赋值给一个char变量
04. **char** a = '中';
05. **char** b = '文';
06. **char** c = '测';
07. **char** d = '试';
08. **char** e = '成';
09. **char** f = '功';
10. System.out.print(a);
11. System.out.print(b);
12. System.out.print(c);
13. System.out.print(d);
14. System.out.print(e);
15. System.out.print(f);
16. }
17. }

```
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代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **public** **class** ChineseTest {
02. **public** **static** **void** main(String\[] args) {
03. // 将一个英文字母赋值给一个byte变量
04. **byte** a = 'a';
05. // 将一个中文汉字赋值给一个byte变量时，编译会报错
06. // byte b = '中';
07. System.out.println("byte a = " + a);
08. // System.out.println("byte b = "+b);
09. }
10. }

```
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代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **public** **class** ChineseTest {
02. **public** **static** **void** main(String\[] args) {
03. // 将一个中文汉字赋值给一个char变量
04. **char** a = '中';
05. **char** b = '文';
06. **char** c = '测';
07. **char** d = '试';
08. **char** e = '成';
09. **char** f = '功';
10. System.out.print(a + b + c + d + e + f);
11. }
12. }

```
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);
	}
}
```

运行结果：

- 156035

这显然不是我们想要的结果。只所以会这样是因为我们误用了“+”运算符，当它被用于字符串和字符串之间，或者字符串和其他类型变量之间时，它产生的效果是字符串的拼接；但当它被用于字符和字符之间时，效果等同于用于数字和数字之间，是一种算术运算。因此我们得到的“156035”是'中'、'文'、'测'、'试'、'成'、'功'这六个汉字分别对应的数值算术相加后的结果。

***字符串的反转输出***

这也是面试题中常考的一道。我们就以一个包含了全部26个英文字母，同时又具有完整含义的最短句子作为例子来完成解答。先来看一下这个句子：

引用

A quick brown fox jumps over the lazy dog.（一只轻巧的棕色狐狸从那条懒狗身上跳了过去。）

最常用的方式就是反向取出每个位置的字符，然后依次将它们输出到控制台：

Java代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **public** **class** StringReverse {
02. **public** **static** **void** main(String\[] args) {
03. // 原始字符串
04. String s = "A quick brown fox jumps over the lazy dog.";
05. System.out.println("原始的字符串：" + s);
06. System.out.print("反转后字符串：");
07. **for** (**int** i = s.length(); i &gt; 0; i--) {
08. System.out.print(s.charAt(i - 1));
09. }
10. // 也可以转换成数组后再反转，不过有点多此一举
11. **char**\[] data = s.toCharArray();
12. System.out.println();
13. System.out.print("反转后字符串：");
14. **for** (**int** i = data.length; i &gt; 0; i--) {
15. System.out.print(data\[i - 1]);
16. }
17. }
18. }

```
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代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **public** **class** StringReverse {
02. **public** **static** **void** main(String\[] args) {
03. // 原始字符串
04. String s = "A quick brown fox jumps over the lazy dog.";
05. System.out.println("原始的字符串：" + s);
06. System.out.print("反转后字符串：");
07. StringBuffer buff = **new** StringBuffer(s);
08. // java.lang.StringBuffer类的reverse()方法可以将字符串反转
09. System.out.println(buff.reverse().toString());
10. }
11. }

```
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代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **import** java.io.UnsupportedEncodingException;
02. **public** **class** EncodeTest {
03. /\**
04. * 打印字符串在指定编码下的字节数和编码名称到控制台
05. \*
06. * @param s
07. *            字符串
08. * @param encodingName
09. *            编码格式
10. \*/
11. **public** **static** **void** printByteLength(String s, String encodingName) {
12. System.out.print("字节数：");
13. **try** {
14. System.out.print(s.getBytes(encodingName).length);
15. } **catch** (UnsupportedEncodingException e) {
16. e.printStackTrace();
17. }
18. System.out.println(";编码：" + encodingName);
19. }
20. **public** **static** **void** main(String\[] args) {
21. String en = "A";
22. String ch = "人";
23. // 计算一个英文字母在各种编码下的字节数
24. System.out.println("英文字母：" + en);
25. EncodeTest.printByteLength(en, "GB2312");
26. EncodeTest.printByteLength(en, "GBK");
27. EncodeTest.printByteLength(en, "GB18030");
28. EncodeTest.printByteLength(en, "ISO-8859-1");
29. EncodeTest.printByteLength(en, "UTF-8");
30. EncodeTest.printByteLength(en, "UTF-16");
31. EncodeTest.printByteLength(en, "UTF-16BE");
32. EncodeTest.printByteLength(en, "UTF-16LE");
33. System.out.println();
34. // 计算一个中文汉字在各种编码下的字节数
35. System.out.println("中文汉字：" + ch);
36. EncodeTest.printByteLength(ch, "GB2312");
37. EncodeTest.printByteLength(ch, "GBK");
38. EncodeTest.printByteLength(ch, "GB18030");
39. EncodeTest.printByteLength(ch, "ISO-8859-1");
40. EncodeTest.printByteLength(ch, "UTF-8");
41. EncodeTest.printByteLength(ch, "UTF-16");
42. EncodeTest.printByteLength(ch, "UTF-16BE");
43. EncodeTest.printByteLength(ch, "UTF-16LE");
44. }
45. }

```
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");
	}
}
```

运行结果如下：

01. 英文字母：A
02. 字节数：1;编码：GB2312
03. 字节数：1;编码：GBK
04. 字节数：1;编码：GB18030
05. 字节数：1;编码：ISO-8859-1
06. 字节数：1;编码：UTF-8
07. 字节数：4;编码：UTF-16
08. 字节数：2;编码：UTF-16BE
09. 字节数：2;编码：UTF-16LE
10. 中文汉字：人
11. 字节数：2;编码：GB2312
12. 字节数：2;编码：GBK
13. 字节数：2;编码：GB18030
14. 字节数：1;编码：ISO-8859-1
15. 字节数：3;编码：UTF-8
16. 字节数：4;编码：UTF-16
17. 字节数：2;编码：UTF-16BE
18. 字节数：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编码为例来进行解答。

如果我们直接按照字节截取会出现什么情况呢？我们来测试一下：

Java代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **import** java.io.UnsupportedEncodingException;
02. **public** **class** CutString {
03. **public** **static** **void** main(String\[] args) **throws** UnsupportedEncodingException {
04. String s = "我ZWR爱JAVA";
05. // 获取GBK编码下的字节数据
06. **byte**\[] data = s.getBytes("GBK");
07. **byte**\[] tmp = **new** **byte**\[6];
08. // 将data数组的前六个字节拷贝到tmp数组中
09. System.arraycopy(data, 0, tmp, 0, 6);
10. // 将截取到的前六个字节以字符串形式输出到控制台
11. s = **new** String(tmp);
12. System.out.println(s);
13. }
14. }

```
import java.io.UnsupportedEncodingException;   
  
public class CutString {   
    public static void main(String[] args) throws UnsupportedEncodingException {   
        String s = "我ZWR爱JAVA";   
        // 获取GBK编码下的字节数据   
        byte[] data = s.getBytes("GBK");   
        byte[] tmp = new byte[6];   
        // 将data数组的前六个字节拷贝到tmp数组中   
        System.arraycopy(data, 0, tmp, 0, 6);   
        // 将截取到的前六个字节以字符串形式输出到控制台   
        s = new String(tmp);   
        System.out.println(s);   
    }   
} 
```

输出结果：

1. 我ZWR?

在截取前六个字节时，第二个汉字“爱”被截取了一半，导致它无法正常显示了，这样显然是有问题的。

我们不能直接使用String类的substring(int beginIndex, int endIndex)方法，因为它是按字符截取的。'我'和'Z'都被作为一个字符来看待，length都是1。实际上我们只要能区分开中文汉字和英文字母，这个问题就迎刃而解了，而它们的区别就是，中文汉字是两个字节，英文字母是一个字节。

Java代码 [![复制代码](http://zangweiren.iteye.com/images/icon_copy.gif)](http://zangweiren.iteye.com/blog/216005 "复制代码")

01. **import** java.io.UnsupportedEncodingException;
02. **public** **class** CutString {
03. /\**
04. * 判断是否是一个中文汉字
05. \*
06. * @param c
07. *            字符
08. * @return true表示是中文汉字，false表示是英文字母
09. * @throws UnsupportedEncodingException
10. *             使用了JAVA不支持的编码格式
11. \*/
12. **public** **static** **boolean** isChineseChar(**char** c)
13. **throws** UnsupportedEncodingException {
14. // 如果字节数大于1，是汉字
15. // 以这种方式区别英文字母和中文汉字并不是十分严谨，但在这个题目中，这样判断已经足够了
16. **return** String.valueOf(c).getBytes("GBK").length &gt; 1;
17. }
18. /\**
19. * 按字节截取字符串
20. \*
21. * @param orignal
22. *            原始字符串
23. * @param count
24. *            截取位数
25. * @return 截取后的字符串
26. * @throws UnsupportedEncodingException
27. *             使用了JAVA不支持的编码格式
28. \*/
29. **public** **static** String substring(String orignal, **int** count)
30. **throws** UnsupportedEncodingException {
31. // 原始字符不为null，也不是空字符串
32. **if** (orignal != **null** && !"".equals(orignal)) {
33. // 将原始字符串转换为GBK编码格式
34. orignal = **new** String(orignal.getBytes(), "GBK");
35. // 要截取的字节数大于0，且小于原始字符串的字节数
36. **if** (count &gt; 0 && count &lt; orignal.getBytes("GBK").length) {
37. StringBuffer buff = **new** StringBuffer();
38. **char** c;
39. **for** (**int** i = 0; i &lt; count; i++) {
40. // charAt(int index)也是按照字符来分解字符串的
41. c = orignal.charAt(i);
42. buff.append(c);
43. **if** (CutString.isChineseChar(c)) {
44. // 遇到中文汉字，截取字节总数减1
45. --count;
46. }
47. }
48. **return** buff.toString();
49. }
50. }
51. **return** orignal;
52. }
53. **public** **static** **void** main(String\[] args) {
54. // 原始字符串
55. String s = "我ZWR爱JAVA";
56. System.out.println("原始字符串：" + s);
57. **try** {
58. System.out.println("截取前1位：" + CutString.substring(s, 1));
59. System.out.println("截取前2位：" + CutString.substring(s, 2));
60. System.out.println("截取前4位：" + CutString.substring(s, 4));
61. System.out.println("截取前6位：" + CutString.substring(s, 6));
62. } **catch** (UnsupportedEncodingException e) {
63. e.printStackTrace();
64. }
65. }
66. }

```
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，也不是空字符串
		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++) {
					// charAt(int index)也是按照字符来分解字符串的
					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爱

向原作者致敬，转自：

作者：臧圩人（zangweiren）  
网址：[http://zangweiren.iteye.com](http://zangweiren.iteye.com/)