/*
 *    Copyright 2012-2013 The Haohui Network Corporation
 */
package com.haohui.common.utils;

/**
 * <pre>
 * 字符串辅助工具
 * </pre>
 * 
 * @project baidamei
 * @author cevencheng <cevencheng@gmail.com>
 * @create 2012-11-30 下午2:42:56
 */
public class StringTool {

	/**
	 *<b>截取指定字节长度的字符串,不能返回半个汉字</b>
	 *	例如:
	 *	如果网页最多能显示17个汉字,那么 length 则为 34
	 * StringTool.getSubString(str, 34);
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String getSubString(String str, int length) {
		int count = 0;
		int offset = 0;
		char[] c = str.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] > 256) {
				offset = 2;
				count += 2;
			} else {
				offset = 1;
				count++;
			}
			if (count == length) {
				return str.substring(0, i + 1);
			}
			if ((count == length + 1 && offset == 2)) {
				return str.substring(0, i);
			}
		}
		return "";
	}
}
最近下载更多
lenovo0101  LV2 2022年8月17日
rekrap  LV1 2020年9月23日
那时年少_1  LV4 2020年9月4日
小资李  LV13 2019年9月17日
chengyongchuan  LV1 2019年8月19日
armuarmu  LV2 2016年10月7日
爷爷在此  LV16 2016年9月27日
zhudiyuan  LV13 2016年9月14日
wk19900206  LV1 2016年6月12日
526870237  LV13 2016年3月20日
最近浏览更多
3334004690  LV10 5月28日
1659552772  LV1 2023年5月9日
唯一&Mike  LV3 2022年10月27日
lenovo0101  LV2 2022年8月17日
rekrap  LV1 2020年9月23日
那时年少_1  LV4 2020年9月4日
jiukankan  LV1 2020年8月18日
唐寒枫  LV1 2020年5月11日
xuyongff  LV24 2019年11月30日
幻羽揚  LV4 2019年11月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友