日期:2014-05-20 浏览次数:20838 次
import java.io.UnsupportedEncodingException; public class StringSpliter { static void getword(String str, int length) { try { byte[] bt = str.getBytes("gbk"); if (bt[length] < 0) { System.out.println(new String(bt, 0, --length, "gbk")); } else { System.out.println(new String(bt, 0, length, "gbk")); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public static void main(String[] args) { String str = "adsf爱上嘎sdf"; StringSpliter.leftStr(str, 6); } }
------解决方案--------------------
public class TestCut { static String s = "adsf爱上嘎sdf"; public static void main(String[] args) { for (int len = 0; len < 12; len++) { System.out.println("长度为"+ len + " : "+ getword(s,len));//分别对长度为0,1,...11进行测试 } } static String getword(String s , int t){ StringBuffer sb = new StringBuffer(); for(int i=0; i<s.length()&&i<t; i++){ char c = s.charAt(i); if(isChinese(c)){ i++;//多加一次 if(i<t)sb.append(c); } else sb.append(c); } return sb.toString(); } // /判断字符是否属于中文 // GENERAL_PUNCTUATION 判断中文的“号 // CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号 // HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号 public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION//"" || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION//. || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {//, return true; } return false; } } /*output: 长度为0 : 长度为1 : a 长度为2 : ad 长度为3 : ads 长度为4 : adsf 长度为5 : adsf 长度为6 : adsf爱 长度为7 : adsf爱 长度为8 : adsf爱嘎 长度为9 : adsf爱嘎d 长度为10 : adsf爱嘎df 长度为11 : adsf爱嘎df */