package com.test;
/**
* @ClassName: Test2
* @Description: TODO
* @author
* @date 2014-5-24
*
*/
public class Test2 {
public static void main(String[] args) throws Exception{
String s="一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十";
System.out.println(substring2(s,9));
}
/**
*
* @Title: isChineseChar
* @Description: TODO
* @param @param c
* @param @return
* @param @throws Exception
* @return boolean
* @throws
*/
public static boolean isChineseChar(char c) throws Exception {
// 如果字节数大于1,是汉字
return String.valueOf(c).getBytes("GBK").length > 1;
}
/**
*
* @Title: 拆分字符串按照固定字节
* @Description: TODO
* @param @param
* 原字符串
* @param @param
* 字节长度
* @param @return
* @param @throws Exception
* @return 新字符串
* @throws
*/
public static String substring2(String orignal,int count)throws Exception{
StringBuffer buff = new StringBuffer();
char c;
int j=0;
int count_tmp=count;
int num=orignal.getBytes("GBK").length;
for (int i = 0; i <num; i++) {
c = orignal.charAt(i);
buff.append(c);
if (isChineseChar(c)) {
// 遇到中文汉字,截取字节总数减1,字节数加1
num--;
j++;
}
j++;
if(j%count_tmp==0||j%(count_tmp+1)==0||j%(count_tmp-1)==0){
count_tmp+=count;
buff.append("\n");
}
}
return buff.toString();
}
public static String substring(String orignal,int count)throws Exception{
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
c = orignal.charAt(i);
buff.append(c);
if (isChineseChar(c)) {
// 遇到中文汉字,截取字节总数减1
--count;
}
}
return buff.toString();
}
}
/**
* @ClassName: Test2
* @Description: TODO
* @author
* @date 2014-5-24
*
*/
public class Test2 {
public static void main(String[] args) throws Exception{
String s="一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十";
System.out.println(substring2(s,9));
}
/**
*
* @Title: isChineseChar
* @Description: TODO
* @param @param c
* @param @return
* @param @throws Exception
* @return boolean
* @throws
*/
public static boolean isChineseChar(char c) throws Exception {
// 如果字节数大于1,是汉字
return String.valueOf(c).getBytes("GBK").length > 1;
}
/**
*
* @Title: 拆分字符串按照固定字节
* @Description: TODO
* @param @param
* 原字符串
* @param @param
* 字节长度
* @param @return
* @param @throws Exception
* @return 新字符串
* @throws
*/
public static String substring2(String orignal,int count)throws Exception{
StringBuffer buff = new StringBuffer();
char c;
int j=0;
int count_tmp=count;
int num=orignal.getBytes("GBK").length;
for (int i = 0; i <num; i++) {
c = orignal.charAt(i);
buff.append(c);
if (isChineseChar(c)) {
// 遇到中文汉字,截取字节总数减1,字节数加1
num--;
j++;
}
j++;
if(j%count_tmp==0||j%(count_tmp+1)==0||j%(count_tmp-1)==0){
count_tmp+=count;
buff.append("\n");
}
}
return buff.toString();
}
public static String substring(String orignal,int count)throws Exception{
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
c = orignal.charAt(i);
buff.append(c);
if (isChineseChar(c)) {
// 遇到中文汉字,截取字节总数减1
--count;
}
}
return buff.toString();
}
}