一个wap的网站
计划是每个页面显示300个字符,用substring这个函数来完成截取字符串的功能。可是有的页面最后一个字(中文)被拆分成了两个问号,分别显示在前一个页的末尾,和后一个页的开头。
我做过多次的试验,用substring截取字符串,无论是中文的,英文的,中英文混合的,甚至再混合上标点符号的,都能正常截取。后来我想可能是转义符的问题,又试了一下,发现如果是"\n"就没问题"\r"就不能正常截取,于是把字符串里的"\r"都replace成"\n",可是还是不行,没辙了。

解决方案 »

  1.   

    以下是jsp页面的相关部分代码
    <%
    final static int cntCharPerPage=300;//一页的最大内容数String info=null;
    int cnoffset=cntpg*cntCharPerPage;
    int cnPages=0;
    info=cnt.getInfo();
    cnPages=info.length()/cntCharPerPage+(info.length()%cntCharPerPage==0?0:1);info=info.substring(cnoffset,(cnoffset+cntCharPerPage)<info.length()?(cnoffset+cntCharPerPage):info.length());
    if(!cnt.isLowInfo())info=new String((StrConvertor.ConvertToXml(info)).getBytes("ISO-8859-1"),"GBK");%>
    StrConvertor的ConvertToXml函数代码如下:
        public static String ConvertToXml(String string)
        {
            string = string.replaceAll("&", "&amp;");
            string = string.replaceAll(">", "&gt;");
            string = string.replaceAll("<", "&lt;");
            string = string.replaceAll("'", "&apos;");
            string = string.replaceAll("\"", "&quot;");
            string = string.replaceAll("\r\n", "\n");
            string = string.replaceAll("\n", "<br/>\r\n");
            return string;
        }
      

  2.   

    现在的问题不是300或者几百个字符的问题,你试过其他小于300的数字吗?我觉得应该是getBytes("iso-8859-1")的问题你可以试试这个例子
            String a = "asdf你好" ;
            byte[] b1 = a.getBytes() ;
            byte[] b2 = a.getBytes("iso-8859-1") ;
            
            System.out.println(b1.length);
            System.out.println(b2.length);
            
            System.out.println(new String(b1));
            System.out.println(new String(b2));这个测试段的输出是:
    8
    6
    asdf你好
    asdf??我再想想先
      

  3.   

    直接用getBytes()的方法不行,我的数据是从数据库取得,不用getBytes("ISO-8859-1")就是乱码
      

  4.   

    if(!cnt.isLowInfo())info=new String((StrConvertor.ConvertToXml(info)).getBytes("ISO-8859-1"),"GBK");你先测试一下不要调用ConvertToXml方法,看看300个字符会不会出现你说的乱码情况
      

  5.   

    我一直用下面的工具解决这个问题,搂住可以试试/**
     * split the String to list by bytes
     * @param orginal string
     * @param maxlength split length.
     * @param is ignore line.
     * @return
     */
    public static List splitStringByByte(String orginal, int maxlength,boolean ignoreLine) { List result = new ArrayList(); if (orginal == null)
    return result;

    orginal = orginal.replaceAll("\r\n", "\n");
    orginal = orginal.replaceAll("\r", "\n");

    StringBuffer sb = new StringBuffer(maxlength); int index = 0; for (int i = 0; i < orginal.length(); i++) {
    char ch = orginal.charAt(i);

    if (ch == '\n') {
    if(ignoreLine){//if ignore line, need convert to \r\n
    if(maxlength<(index+2)){
    result.add(sb.toString()); sb = new StringBuffer(maxlength);

    index = 0;

    continue;

    }else{

    sb.append("\r\n");

    index=index+2;

    continue;
    }
    }else{
    result.add(sb.toString()); sb = new StringBuffer(maxlength);

    index = 0;

    continue;
    }
    } if (isAscii(ch)) {
    index = index + 1;
    } else {
    index = index + 3;
    }
    if (index > maxlength) { result.add(sb.toString()); sb = new StringBuffer(maxlength); index = isAscii(ch) ? 1 : 3; } sb.append(ch);
    } if (sb.length() > 0)
    result.add(sb.toString()); return result;
    }
      

  6.   

    isAscii()的代码是怎么实现的?
      

  7.   

    又有一个新的问题,就是我把楼上给的函数写在类里,编译好,放到tomcat的相应目录下,重启了n次服务,它就就是不认我这个类,我哭了
      

  8.   

    你有其他的类或jar包吗?打在一起试试
      

  9.   

    试过了加在打好的jar包里,也不行,直接把函数放在页面了。果然解决了,谢谢各位了