字符集

运行结果:

  原始的字符串: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值