java 字符串截取的實例詳解
java 字符串截取的實例詳解
前言:
在Java中,字符串“abcd”與字符串“ab你好”的長度是一樣,都是四個字符。
但對應的字節(jié)數(shù)不同,一個漢字占兩個字節(jié)。
定義一個方法,按照指定的字節(jié)數(shù)來取子串。
如:對于“ab你好”,如果取三個字節(jié),那么子串就是ab與“你”字的半個,那么半個就要舍棄。
如果取四個字節(jié)就是“ab你”,取五個字節(jié)還是“ab你”。
僅考慮GBK和utf-8編碼
實例代碼:
import java.io.UnsupportedEncodingException; import org.junit.Test; /** * @author<a href="mailto:953801304@qq.com" rel="external nofollow" >胡龍華</a> * @version 2017-4-4 下午1:08:45 * @fileName StringCut.java */ public class StringCut { @Test public void analyze(){ String str1 = "你好abc"; byte[] bs1=null; byte[] bs2=null; try { bs1 = str1.getBytes("GBK"); System.out.println("---GBK---"); for(byte b:bs1){ System.out.print(b+" "); } System.out.println(); //-60 -29 -70 -61 97 98 99 // 發(fā)現(xiàn)規(guī)律,再gbk中一個中文漢字 都是以兩個字節(jié) 小于0的數(shù)存儲 bs2 = str1.getBytes("utf-8"); System.out.println("---utf-8---"); for(byte b:bs2){ System.out.print(b+" "); } //-28 -67 -96 -27 -91 -67 97 98 99 // 發(fā)現(xiàn)規(guī)律,在utf-8中一個中文漢字 是以三個字節(jié) 小于0 的數(shù)存儲 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * 思路:從第len個往前數(shù),連續(xù)2的倍數(shù)個負數(shù)則全部輸出,單數(shù)個則去掉最后一個輸出 * @param str * @param len * @return */ private static String StringCutByGBK(String str,int len){ byte[] bs = null; try { int count = 0; bs = str .getBytes("GBK"); for(int i=len-1;i>=0;i--){ if(bs[i]<0){ count++; }else{ break; } // 0 1 2 3 4 5 6 7 8 9 10 11 12 } //-60 -29 -70 -61 -80 -95 97 98 99 -76 -17 -72 -25 if(count%2==0){ String s=new String(bs, 0, len, "GBK"); System.out.println("截取"+len+"個字符:"+s); }else{ String s=new String(bs, 0, len-1, "GBK"); System.out.println("截取"+len+"個字符:"+s); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } /** * 思路:從第len個往前數(shù),連續(xù)3的倍數(shù)個負數(shù)則全部輸出,其他情況則去掉最后count%3個輸出 * @param str * @param len * @return */ private static String StringCutByUTF8(String str,int len){ byte[] bs = null; try { int count = 0; bs = str .getBytes("UTF-8"); for(int i=len-1;i>=0;i--){ if(bs[i]<0){ count++; }else{ break; } } // 0 1 2 3 4 5 6 7 8 9 10 11 12 //-60 -29 -70 -61 -80 -95 97 98 99 -76 -17 -72 -25 if(count%3==0){ String s=new String(bs, 0, len, "UTF-8"); System.out.println("截取"+len+"個字符:"+s); }else{ String s=new String(bs, 0, len-count%3, "UTF-8"); System.out.println("截取"+len+"個字符:"+s); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } @Test public void TEST() { String str = "你好啊abc達哥"; try { System.out.println("---測試gbk---"); byte bs [] = str.getBytes("GBK"); for(int i=0;i<=bs.length;i++){ //System.out.print(bs[i]+" "); StringCutByGBK(str,i); } System.out.println("---測試UTF-8---"); byte bs2 [] = str.getBytes("utf-8"); for(int i=0;i<=bs2.length;i++){ //System.out.print(bs[i]+" "); StringCutByUTF8(str,i); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以上就是java 字符串截取的實例,如有疑問請留言或者到本站社區(qū)交流討論,本站關于java的文章還有很多,希望大家多多搜索參閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
SpringCloud微服務剔除下線功能實現(xiàn)原理分析
SpringCloud是一種微服務的框架,利用它我們可以去做分布式服務開發(fā),這篇文章主要介紹了SpringCloud微服務剔除下線功能,需要的朋友可以參考下2022-11-11Java之NoClassDefFoundError的原因及分析
在Java開發(fā)中,經(jīng)常會遇到ClassNotFoundException和NoClassDefFoundError異常,ClassNotFoundException發(fā)生在編譯時JVM無法找到類,而NoClassDefFoundError則發(fā)生在運行時JVM無法加載類,這兩個異常雖然原因相似,但有本質(zhì)區(qū)別2024-09-09SpringBoot基于Redis的分布式鎖實現(xiàn)過程記錄
Redis是一套 key-value 高性能數(shù)據(jù)庫,使用它可以大大提高我們的開發(fā)效率,在SpringBoot中,自動配置也幫我們節(jié)約了大量的配置,下面這篇文章主要給大家介紹了關于SpringBoot基于Redis的分布式鎖實現(xiàn)的相關資料,需要的朋友可以參考下2022-01-01詳解JAVA中ListIterator和Iterator的辨析
這篇文章主要為大家詳細介紹了JAVAListIterator和Iterator的辨析,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02spring boot實現(xiàn)阿里云視頻點播上傳視頻功能(復制粘貼即可)
這篇文章主要介紹了spring boot實現(xiàn)阿里云視頻點播上傳視頻功能(復制粘貼即可),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12