字符集

運行結果:

  原始的字符串:A quick brown fox jumps over the lazy dog.

  反轉後字符串:.god yzal eht revo spmuj xof nworb kciuq A

  反轉後字符串:.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());
	}
}

  運行結果:

  原始的字符串:A quick brown fox jumps over the lazy dog.

  反轉後字符串:.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");
	}
}

  運行結果如下:

  英文字母:A

  字節數:1;編碼:GB2312

  字節數:1;編碼:GBK

  字節數:1;編碼:GB18030

  字節數:1;編碼:ISO-8859-1

  字節數:1;編碼:UTF-8

  字節數:4;編碼:UTF-16

  字節數:2;編碼:UTF-16BE

  字節數:2;編碼:UTF-16LE

  中文漢字:人

  字節數:2;編碼:GB2312

  字節數:2;編碼:GBK

  字節數:2;編碼:GB18030

  字節數:1;編碼:ISO-8859-1

  字節數:3;編碼:UTF-8

  字節數:4;編碼:UTF-16

  字節數:2;編碼:UTF-16BE

  字節數: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編碼爲例來進行解答。

原文:http://tech.ddvip.com/2009-03/1237185440111445_2.html

發佈了4 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章