急求在JSP中截取字符串的方法(中英文混合处理)?我想在首页新闻列表中限制字符长度如10个,但是用substring()来做的话,中文和英文长度导致限制的长度就展示不一样了,要顶满格,改如何处理。急求有没好的JAVA方法提供感谢!

解决方案 »

  1.   

    /*
     * Created on 2005-5-13
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package oa.util;/**
     * @author Administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Truncate {

    public static String truncate(String source, int len, String delim) {
    if (source == null) return null;
    int start, stop , byteLen ;
    int alen = source.getBytes().length;
    //len += len ;
    if (len > 0) {
    if (alen <= len) return source;
    start = stop = byteLen = 0; //TODO effizienter
    //stop = (len > alen ? alen : len);

    while ( byteLen <= len){
    if ( source.substring(stop,stop+1).getBytes().length == 1){
    byteLen += 1 ;
    }else{
    byteLen += 2 ;
    }
    stop++;
    }
    StringBuffer sb = new StringBuffer(source.substring(start, stop-1));
    if (alen > len) sb.append(delim);
    return sb.toString();
    }
    start = (len < -alen ? 0 : alen + len);
    stop = alen;
    StringBuffer sb = new StringBuffer(source.substring(start/2, stop/2));
    if (-alen <= len) sb.insert(0, delim);
    return sb.toString();
    }
    }
      

  2.   

    ASP代码是这样,急求JAVA代码。。'函数名:strLeft
    '作  用:按照num数限制字符串长度。汉字算两个字符,英文算一个字符。
    '参  数:str  ----字符串
    '参  数:num  ----限制num数
    '返回值:字符串长度
    '**************************************************
    function strLeft(str,num)
    dim p_str,p_num
        p_str = ""
        p_num = 0    
    if trim(str) <> "" then
        p_len = len(str)
        for i = 1 to p_len
            if asc(mid(str,i,1)) > 255 or asc(mid(str,i,1)) < 0 then
                p_num = p_num + 2
            else
                p_num = p_num + 1
            end if
            
            if p_num > num then 
                p_str = Left(str,i-1)  
                exit for
            else
                p_str = str
            end if
        next
    end if
    strLeft=p_str
    end function
      

  3.   

    晕,居然没人说得。。只有自己写了。/**
         * JSP中限制字符长度返回串 | 按照num数限制字符串长度。汉字算两个字符,英文算一个字符
         * @param str String
         * @param num int
         * @return String
         */
        public static String getLeftStr(String str, int num) {
            if (str == null || str.equals("")) {
                return "";
            }
            else {
                char[] curCharArray = str.toCharArray();
                int ascNumber = 0;
                int p_num = 0;
                String p_str = "";
                for (int i = 0; i < curCharArray.length; i++) {
                    ascNumber = (int) curCharArray[i];
                    if (ascNumber > 255 || ascNumber < 0) {
                        p_num = p_num + 2;
                    }
                    else {
                        p_num = p_num + 1;
                    }
                    if (p_num > num) {
                        p_str = p_str.substring(0, i);
                        break;
                    }
                    else {
                        p_str = str;
                    }
                }
                return p_str;
            }
        }