最近在做WAP项目,碰到需要对正文进行分页的情况,难点是中英文混排情况下的截字问题。中英文混排时,在GBK编码下,英文占一个字符,中文占两个字符,截的时候容易出现从中文字的第二个字符开始截的情况,截出来的字就会出现乱码。以下贴代码,代码是遍历正文的每个字,边遍历边收集当前页显示内容,同时统计字符数。如果哪位大虾有更好的写法,欢迎留言。
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
/**
* 正文分页类
* @author Sophie_Lin
*
*/
public class ContentPage implements Serializable {
private static final long serialVersionUID = 1L;
protected int pageNo;
protected int pageSize; //正文显示字数(按中文显示字数算)
protected String content;
protected String contentDisp;
protected int totalCount;
protected int totalPages;
public ContentPage( int pageNo, int pageSize,String content){
this.pageNo = pageNo;
this.pageSize = pageSize;
this.content = content;
int start = (pageNo-1)*pageSize*2;
int i=0;
StringBuilder sb = new StringBuilder();
for( char c : this.content.toCharArray()){
try {
if(String.valueOf(c).getBytes( "GBK").length>1){
//中文字,+2
i+=2;
} else{
i++;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i>start && i<=start+pageSize*2){
sb.append(c);
}
}
int totalPages = 0;
if(i%(pageSize*2)==0){
totalPages = i/(pageSize*2);
} else{
totalPages = i/(pageSize*2)+1;
}
this.setContentDisp(sb.toString());
this.setTotalCount(i);
this.setTotalPages(totalPages);
}
public int getPageNo() {
return pageNo;
}
public void setPageNo( int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize( int pageSize) {
this.pageSize = pageSize;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getContentDisp() {
return contentDisp;
}
public void setContentDisp(String contentDisp) {
this.contentDisp = contentDisp;
}
public int getTotalCount(){
return totalCount;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages( int totalPages) {
this.totalPages = totalPages;
}
public void setTotalCount( int totalCount) {
this.totalCount = totalCount;
}
}
import java.io.UnsupportedEncodingException;
/**
* 正文分页类
* @author Sophie_Lin
*
*/
public class ContentPage implements Serializable {
private static final long serialVersionUID = 1L;
protected int pageNo;
protected int pageSize; //正文显示字数(按中文显示字数算)
protected String content;
protected String contentDisp;
protected int totalCount;
protected int totalPages;
public ContentPage( int pageNo, int pageSize,String content){
this.pageNo = pageNo;
this.pageSize = pageSize;
this.content = content;
int start = (pageNo-1)*pageSize*2;
int i=0;
StringBuilder sb = new StringBuilder();
for( char c : this.content.toCharArray()){
try {
if(String.valueOf(c).getBytes( "GBK").length>1){
//中文字,+2
i+=2;
} else{
i++;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i>start && i<=start+pageSize*2){
sb.append(c);
}
}
int totalPages = 0;
if(i%(pageSize*2)==0){
totalPages = i/(pageSize*2);
} else{
totalPages = i/(pageSize*2)+1;
}
this.setContentDisp(sb.toString());
this.setTotalCount(i);
this.setTotalPages(totalPages);
}
public int getPageNo() {
return pageNo;
}
public void setPageNo( int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize( int pageSize) {
this.pageSize = pageSize;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getContentDisp() {
return contentDisp;
}
public void setContentDisp(String contentDisp) {
this.contentDisp = contentDisp;
}
public int getTotalCount(){
return totalCount;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages( int totalPages) {
this.totalPages = totalPages;
}
public void setTotalCount( int totalCount) {
this.totalCount = totalCount;
}
}
转载于:https://blog.51cto.com/mmqzlj/563633