java 按字节计算字符串的长度,按字节截取字符串

本文介绍了一个用于处理Java中英文字符串字节长度的工具类,包括获取字符串字节长度的方法及按字节截取字符串的功能。详细解释了如何在不同编码下计算中文占用的字节数,并提供了实际应用示例。

 

import java.io.UnsupportedEncodingException;

/**
 * 字符串工具类
 * @author happyqing
 * @since 2013.11.6
 */
public class StringUtil {

	/**
	 * 返回中英文字符串的字节长度
	 * 
	 * @param str
	 * @return
	 */
	public static int getLength(String str) {
		try {
			return str.getBytes("UTF-8").length; // 一个中文占3个字节。
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 返回中英文字符串的字节长度
	 * 
	 * @param str
	 * @return
	 */
	public static int getStrLength(String str) {
		if (str == null || str.length() == 0) {
			return 0;
		}
		int len = 0;
		for (int i=0; i<str.length(); i++ ) {
			//UTF-8编码格式中文占三个字节,GBK编码格式 中文占两个字节 ;
			len += (str.charAt(i)>255 ? 3 : 1);
		}
		return len;
	}
	
	/**
	 * 按字节截取字符串
	 * @param str
	 * @param bytes
	 * @return
	 */
	public static String subStringByBytes(String str, int bytes) {
		if (str == null || str.length() == 0) {
			return str;
		}
		int len = 0;
		for (int i=0; i<str.length(); i++ ) {
			//GBK 编码格式 中文占两个字节 UTF-8 编码格式中文占三个字节;
			len += (str.charAt(i)>255 ? 3 : 1); 
			
			if(len>bytes){
				return str.substring(0,i);
			}
		}
		return str;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("中文a".getBytes("UTF-8").length); // 7
			System.out.println(StringUtil.getStrLength("中文a")); // 7
			System.out.println("中文a".length()); // 3
			System.out.println(subStringByBytes("中文a",4)); //中
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}@author happyqing
 * @since 2013.11.6
 */
public class StringUtil {

	/**
	 * 返回中英文字符串的字节长度
	 * 
	 * @param str
	 * @return
	 */
	public static int getLength(String str) {
		try {
			return str.getBytes("UTF-8").length; // 一个中文占3个字节。
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 返回中英文字符串的字节长度
	 * 
	 * @param str
	 * @return
	 */
	public static int getStrLength(String str) {
		if (str == null || str.length() == 0) {
			return 0;
		}
		int len = 0;
		for (int i=0; i<str.length(); i++ ) {
			//UTF-8编码格式中文占三个字节,GBK编码格式 中文占两个字节 ;
			len += (str.charAt(i)>255 ? 3 : 1);
		}
		return len;
	}
	
	/**
	 * 按字节截取字符串
	 * @param str
	 * @param bytes
	 * @return
	 */
	public static String subStringByBytes(String str, int bytes) {
		if (str == null || str.length() == 0) {
			return str;
		}
		int len = 0;
		for (int i=0; i<str.length(); i++ ) {
			//GBK 编码格式 中文占两个字节 UTF-8 编码格式中文占三个字节;
			len += (str.charAt(i)>255 ? 3 : 1); 
			
			if(len>bytes){
				return str.substring(0,i);
			}
		}
		return str;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			System.out.println("中文a".getBytes("UTF-8").length); // 7
			System.out.println(StringUtil.getStrLength("中文a")); // 7
			System.out.println("中文a".length()); // 3
			System.out.println(subStringByBytes("中文a",4)); //中
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

JS按字节计算字符串长度,按字节截取字符串
http://happyqing.iteye.com/blog/1979816

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值