/*
* content內容過長可能會導致xml檔案過大,載入太慢。
* 但從seo的角度考慮全部輸出有利於搜尋引擎,但一般情況下內容也不會太多
* 為防止空格換行css無法控制撐大頁面,用Regex替換掉空格,所以截取前面100個字元,頁面顯示的內容多少用css控制
*zdz的作品,流風的作品
*/
//str.trim().replaceAll("\s+"," ");
String contenttemp = rs.getString(contentName).trim().replaceAll("\s+","");
//NpfDebug.print(contenttemp.length());
if(contenttemp.length()>100){//如果長度大於100則截取
contenttemp = contenttemp.substring(0, 100);
//NpfDebug.print("contenttemp.length()>100 ? "+contenttemp.length()+"n"+contentStr);
}
rsbody.append(beforCONTENT);
rsbody.append(contenttemp);
rsbody.append(endCONTENT);
----------------------
開發中經常遇到,字串過長,無法完全顯示的問題
這時候就需要截取我們所需要的長度,後面顯示省略符號或其他字元。
由於中文字元佔兩個位元組,而英文字元佔用一個位元組,所以,單純地判斷字元數,效果往往不盡如人意
下面的方法通過判斷字元的類型來進行截取,效果還算可以:)
如果大家有其他的解決方案歡迎貼出來,共同學習:)
**********************************************************************
| 代碼如下 |
複製代碼 |
|
private String str;
private int counterOfDoubleByte;
private byte b[];
/**
* 設定需要被限制長度的字串
* @param str 需要被限制長度的字串
*/
public void setLimitLengthString(String str){
this.str = str;
}
/**
* @param len 需要顯示的長度(<font color="red">注意:長度是以byte為單位的,一個漢字是2個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;
}
-------------------
/** *//**
* 按位元組長度截取字串
* @param str 將要截取的字串參數
* @param toCount 截取的位元組長度
* @param more 字串末尾補上的字串
* @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個字元
*
* @param str 被處理字串
* @param toCount 截取長度
* @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;
} |
------------------------
得到字串真實長度和取固定長度的字串函數
// 截取固定長度子字串 sSource為字串iLen為長度
| 代碼如下 |
複製代碼 |
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;
} |