一个wap的网站
计划是每个页面显示300个字符,用substring这个函数来完成截取字符串的功能。可是有的页面最后一个字(中文)被拆分成了两个问号,分别显示在前一个页的末尾,和后一个页的开头。
我做过多次的试验,用substring截取字符串,无论是中文的,英文的,中英文混合的,甚至再混合上标点符号的,都能正常截取。后来我想可能是转义符的问题,又试了一下,发现如果是"\n"就没问题"\r"就不能正常截取,于是把字符串里的"\r"都replace成"\n",可是还是不行,没辙了。
解决方案 »
- 大家帮我找找这个网站的缺点 http://www.odl-fx.cn/
- jsp 里面window.showModalDialog带了参数就进不去Action,求帮忙!!!
- 用Java_实现Web自动生成类。
- php接收不到jsp通过 url传过来的参数(含中文)该怎么办呢?
- 什么是j2ee
- 制作javabean出错,求解~!
- 如何向服务器上传文件(急,在线等)
- jsp分页
- 不要小看这个问题啊,页面跳转最新问题版!
- 为什么我打开任何一个csdn论坛的文章都显示乱玛。是csdn的问题,还是我的电脑中毒了,大家遇到了么?
- Spring+Hibernate保存数据不提交的问题
- java中字符串的替换问题,如何把路径中的"\"换成"/"或者"\\",在线等
<%
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("&", "&");
string = string.replaceAll(">", ">");
string = string.replaceAll("<", "<");
string = string.replaceAll("'", "'");
string = string.replaceAll("\"", """);
string = string.replaceAll("\r\n", "\n");
string = string.replaceAll("\n", "<br/>\r\n");
return string;
}
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??我再想想先
* 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;
}