public class SplitMessage {
static SplitMessage splitMessage=new SplitMessage();
static boolean bChineseFirstHalf = false;
static int messagePage=1;
public int trimGBK(byte[] buf, int n){
int num = 0;
for(int i=0;i<n;i++)
{
if(buf[i]<0 && !bChineseFirstHalf){
bChineseFirstHalf = true;
}else{
num++;
bChineseFirstHalf = false;
}
}
return num;
}
public void splitMessage(String str) throws UnsupportedEncodingException{
int strBytesLength=str.getBytes("GBK").length;
int num = 0;
if(strBytesLength/140>0&&strBytesLength%140!=0){
num = trimGBK(str.getBytes("GBK"), 140);
splitMessage.sendMessage(messagePage, str.substring(0, num));
messagePage++;
splitMessage(str.substring(num));
}
else{
num = trimGBK(str.getBytes("GBK"), strBytesLength);
splitMessage.sendMessage(messagePage, str.substring(0, num));
}
}
private void sendMessage(int page, String subString ){
System.out.println("第"+messagePage + "页:"+ subString);
}
public static void main(String[] args) throws UnsupportedEncodingException{
String str = "我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢";
String str0 = "我爱javaSE你呢我爱javaSE";
String str1= "我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javasSE你呢sdf";
splitMessage.splitMessage(str);
}
}
static SplitMessage splitMessage=new SplitMessage();
static boolean bChineseFirstHalf = false;
static int messagePage=1;
public int trimGBK(byte[] buf, int n){
int num = 0;
for(int i=0;i<n;i++)
{
if(buf[i]<0 && !bChineseFirstHalf){
bChineseFirstHalf = true;
}else{
num++;
bChineseFirstHalf = false;
}
}
return num;
}
public void splitMessage(String str) throws UnsupportedEncodingException{
int strBytesLength=str.getBytes("GBK").length;
int num = 0;
if(strBytesLength/140>0&&strBytesLength%140!=0){
num = trimGBK(str.getBytes("GBK"), 140);
splitMessage.sendMessage(messagePage, str.substring(0, num));
messagePage++;
splitMessage(str.substring(num));
}
else{
num = trimGBK(str.getBytes("GBK"), strBytesLength);
splitMessage.sendMessage(messagePage, str.substring(0, num));
}
}
private void sendMessage(int page, String subString ){
System.out.println("第"+messagePage + "页:"+ subString);
}
public static void main(String[] args) throws UnsupportedEncodingException{
String str = "我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢";
String str0 = "我爱javaSE你呢我爱javaSE";
String str1= "我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢" +
"我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javaSE你呢我爱javasSE你呢sdf";
splitMessage.splitMessage(str);
}
}