`
softlife
  • 浏览: 109184 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA按字符长度截取中英文混合字符串

    博客分类:
  • Java
阅读更多
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test t = new Test();
		System.out.println(t.splitStr("雪雪xx儿儿ee雪儿rr", 4));

	}

	public String splitStr(String sChars, int sLength) {
		String str = "";
		int sl = 0;
		for (int i = 0; i < sChars.length(); i++) {
			if ((sChars.charAt(i) + "").equals(""))
				break;
			str += sChars.charAt(i);
			sl += getStrLength(sChars.charAt(i) + "");
			if (sl>= sLength) {
				str += ",";
				sl=0;
			}
		}
		return str;
	}

	public int getStrLength(String s) {
		int length = 0;
		for (int i = 0; i < s.length(); i++) {
			int ascii = Character.codePointAt(s, i);
			if (ascii >= 0 && ascii <= 255)
				length++;
			else
				length += 2;

		}
		return length;
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics