短信内容根据字节分拆
最近做个项目,把长短信内容分拆为多条短信,按字节160个分拆,由于汉字和全角的符号截取不当造成乱码,参考了网上一篇文章,但是那文章考虑得不是很周全,所以我修改了局部代码,最后终于完成我要的效果了,一开始有考虑过正则表达式,JAVA是有支持的,只是我觉得效率可能还不如我这样写,各位朋友也可以尝试一下别的方法来实现,以下代码是适用C++。
package cn.gzjp.crbt.common.util;import java.io.UnsupportedEncodingException;/** * * 功能描述:字符串根据字节分拆,用于短信内容分拆* <p>版权所有:金鹏科技* <p>未经本公司许可,不得以任何方式复制或使用本程序任何部分** @author zhangjh 新增日期:2010-12-22* @author 你的姓名 修改日期:2010-12-22* @since zte_crbt_bi */public class SplitString {private String str;private int bytes;private byte[] strBytes;private byte[] result;private byte[] another;// 字节大于127,值是负数private byte border = 0;public SplitString(String str, int bytes) {try {this.str = str;byte[] s = str.getBytes("GBK");if(s.length<bytes)this.bytes = s.length;elsethis.bytes = bytes;} catch (UnsupportedEncodingException e) {e.printStackTrace();}//System.out.println("The String is: " + str + " ; Bytes is : " + bytes);}public String split() {try {strBytes = str.getBytes("GBK");//System.out.println("strBytes.length:"+strBytes.length+",bytes:"+bytes);result = new byte[bytes];another = new byte[bytes - 1];boolean flag = false;int icount =0;for (int i = 0; i < result.length; i++) {if (strBytes[i] < 0) {if (icount % 2 == 0) {flag = true;} else {flag = false;}icount++;}result[i] = strBytes[i];}//System.out.println("flag:"+flag);// 判断是否汉字第一个字符(无符号值>127;带符号则<0,为负数!)if (flag && result[result.length - 1] < border) {if (!flag &&result[result.length - 2] < border) {//System.out.println("1."+new String(result,"GBK"));return new String(result,"GBK");}for (int i = 0; i < another.length; i++) {another[i] = result[i];}//System.out.println("2."+new String(another,"GBK"));return new String(another,"GBK");} else {//System.out.println("3."+new String(result,"GBK"));return new String(result,"GBK");}} catch (UnsupportedEncodingException e) {e.printStackTrace();} return null;}public static void main(String[] args) throws UnsupportedEncodingException {long startTime =System.currentTimeMillis();SplitString amao;String str = "¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds¥##,ABC汉DEFsds";int start=0,end=0;int index=0;while(str.length()>0){amao = new SplitString(str, 160);String ll = amao.split();byte[] bytes = ll.getBytes("GBK");//System.out.println(bytes.length+":"+ll);start = ll.length();end = str.length();str = str.substring(start,end);System.out.println(index+","+ll);index++;}System.out.println("use time:"+(System.currentTimeMillis()-startTime));}}