写了个截取中文字符串的方法

public static String substring(String value, int beginIndex, int length) {
		String chinese = "[\u0391-\uFFE5]";
		if (length > value.length()) {
			throw new IllegalArgumentException(
					"length must less than value.length()");
		}
		char[] charArray = value.toCharArray();
		StringBuilder sb = new StringBuilder();
		for (int i = beginIndex,twice=0; twice < length; i++,twice++) {
			/* 获取一个字符 */
			String temp = String.valueOf(charArray[i]);
			/* 如果是中文多增加一个字符 */
			if (temp.matches(chinese)) {
				if(length-twice>1){
					++twice;
					sb.append(temp);
				}
			} else {
				sb.append(temp);
			}
		}
		return sb.toString();
	}
 

猜你喜欢

转载自san-yun.iteye.com/blog/1535090