Java精確截取字符串
Java精確截取字符串,取得字符串前面指定長(zhǎng)度字符函數(shù)
用java取得字符串的前面部分內(nèi)容的函數(shù)contentStr = contenttemp.substring(0, 150);其中要保證***長(zhǎng)度不能超過(guò)字符串的長(zhǎng)度。下面是我的實(shí)現(xiàn)部分代碼,以及網(wǎng)上搜索的相關(guān)代碼:
- /*
- * content內(nèi)容過(guò)長(zhǎng)可能會(huì)導(dǎo)致xml文件過(guò)大,加載太慢。
- * 但從seo的角度考慮全部輸出有利于搜索引擎,但一般情況下內(nèi)容也不會(huì)太多
- * 為防止空格換行css無(wú)法控制撐大頁(yè)面,用正則表達(dá)式替換掉空格,所以截取前面100個(gè)字符,頁(yè)面顯示的內(nèi)容多少用css控制
- *zdz的作品,流風(fēng)的作品
- */
- //str.trim().replaceAll("\\s+"," ");
- String contenttemp = rs.getString(contentName).trim().replaceAll("\\s+","");
- //NpfDebug.print(contenttemp.length());
- if(contenttemp.length()>100){//如果長(zhǎng)度大于100則截取
- contenttemp = contenttemp.substring(0, 100);
- //NpfDebug.print("contenttemp.length()>100 ? "+contenttemp.length()+"\n"+contentStr);
- }
- rsbody.append(beforCONTENT);
- rsbody.append(contenttemp);
- rsbody.append(endCONTENT);
開發(fā)中經(jīng)常遇到,字符串過(guò)長(zhǎng),無(wú)法完全顯示的問(wèn)題
這時(shí)候就需要截取我們所需要的長(zhǎng)度,后面顯示省略號(hào)或其他字符。
由于中文字符占兩個(gè)字節(jié),而英文字符占用一個(gè)字節(jié),所以,單純地判斷字符數(shù),效果往往不盡如人意
下面的方法通過(guò)判斷字符的類型來(lái)進(jìn)行截取,效果還算可以:)
如果大家有其他的解決方法歡迎貼出來(lái),共同學(xué)習(xí):)
- private String str;
- private int counterOfDoubleByte;
- private byte b[];
- /**
- * 設(shè)置需要被限制長(zhǎng)度的字符串
- * @param str 需要被限制長(zhǎng)度的字符串
- */
- public void setLimitLengthString(String str){
- this.str = str;
- }
- /**
- * @param len 需要顯示的長(zhǎng)度(<font color="red">注意:長(zhǎng)度是以byte為單位的,一個(gè)漢字是2個(gè)byte</font>)
- * @param symbol 用于表示省略的信息的字符,如“...”,“>>>”等。
- * @return 返回處理后的字符串
- */
- public String getLimitLengthString(int len, String symbol) throws UnsupportedEncodingException {
- counterOfDoubleByte = 0;
- 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)
- return new String(b, 0, len, "GBK") + symbol;
- else
- return new String(b, 0, len - 1, "GBK") + symbol;
- }
- -------------------
- /** *//**
- * 按字節(jié)長(zhǎng)度截取字符串
- * @param str 將要截取的字符串參數(shù)
- * @param toCount 截取的字節(jié)長(zhǎng)度
- * @param more 字符串末尾補(bǔ)上的字符串
- * @return 返回截取后的字符串
- */
- public String substring(String str, int toCount, String more) ...{
- int reInt = 0;
- String reStr = "";
- if (str == null)
- return "";
- char[] tempChar = str.toCharArray();
- for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) ...{
- String s1 = str.valueOf(tempChar[kk]);
- byte[] b = s1.getBytes();
- reInt += b.length;
- reStr += tempChar[kk];
- }
- if (toCount == reInt || (toCount == reInt - 1))
- reStr += more;
- return reStr;
- }
- =================
- /**
- * 取字符串的前toCount個(gè)字符
- *
- * @param str 被處理字符串
- * @param toCount 截取長(zhǎng)度
- * @param more 后綴字符串
- * @version 2004.11.24
- * @author zhulx
- * @return String
- */
- public static String substring(String str, int toCount,String more)
- {
- int reInt = 0;
- String reStr = "";
- if (str == null)
- return "";
- char[] tempChar = str.toCharArray();
- for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++) {
- String s1 = str.valueOf(tempChar[kk]);
- byte[] b = s1.getBytes();
- reInt += b.length;
- reStr += tempChar[kk];
- }
- if (toCount == reInt || (toCount == reInt - 1))
- reStr += more;
- return reStr;
- }
得到字符串真實(shí)長(zhǎng)度和取固定長(zhǎng)度的字符串函數(shù)
- // 截取固定長(zhǎng)度子字符串 sSource為字符串iLen為長(zhǎng)度
- function getInterceptedStr(sSource, iLen)
- {
- if(sSource.replace(/[^\x00-\xff]/g,"xx").length <= iLen)
- {
- return sSource;
- }
- var ELIDED = "";
- var str = "";
- var l = 0;
- var schar;
- for(var i=0; schar=sSource.charAt(i); i++)
- {
- str += schar;
- l += (schar.match(/[^\x00-\xff]/) != null ? 2 : 1);
- if(l >= iLen - ELIDED.length)
- {
- break;
- }
- }
- str += ELIDED;
- return str;
- }
原文鏈接:http://www.cnblogs.com/myphoebe/archive/2011/12/20/2294171.html
【編輯推薦】