首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

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

2012-10-06 
写了个截取中文字符串的方法public static String substring(String value, int beginIndex, int length)

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

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

热点排行