读书人

写了个截取中文字符串的步骤

发布时间: 2012-10-06 17:34:01 作者: rapoo

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

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();}
?

读书人网 >其他相关

热点推荐