java 中文怎么截取,java String 中文 字符串 截取
/** ?? ? * @param len ?? ? *??????????? 需要顯示的長度(注意:長度是以byte為單位的,一個漢字是2個byte) ?? ? * @param symbol ?? ? *??????????? 用于表示省略的信息的字符,如“...”,“>>>”等。 ?? ? * @return 返回處理后的字符串 ?? ? */ ?? ?public static String getLimitLengthString(String str) { ?? ??? ?String symbol="..."; ?? ??? ?int len = 25; //?? ??? ?int counterOfDoubleByte; ?? ??? ?byte b[]; ?? ??? ?String result=""; //?? ??? ?counterOfDoubleByte = 0; ?? ??? ?try { ?? ??? ??? ?b = str.getBytes("GBK"); ?? ??? ??? ?if (b.length <= len) ?? ??? ??? ??? ?return str; //?? ??? ??? ?for (int i = 0; i < len; i++) { //?? ??? ??? ??? ?if (b[i] < 0) //?? ??? ??? ??? ??? ?counterOfDoubleByte++; //?? ??? ??? ?} // //?? ??? ??? ?if (counterOfDoubleByte % 2 == 0) { //?? ??? ??? ??? ?result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK"); //?? ??? ??? ?} else { //?? ??? ??? ??? ?result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK"); //?? ??? ??? ?} ?? ??? ??? ?result=new String(b, 0, 10, "GBK")+symbol+new String(b, b.length-12, 12, "GBK"); ?? ??? ?} catch (UnsupportedEncodingException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ?return result; ?? ?}
總結
以上是生活随笔為你收集整理的java 中文怎么截取,java String 中文 字符串 截取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面试题php2018,2018php最新
- 下一篇: matlab潮流程序,IEEE33节点m