概述
最近项目上,需求,需要Android端在一段字符串分包处理,在此做个笔录。
1、code
//public class Main {
// public static void main(String[] args) {
// System.out.println("Hello world!");
// }
//}
/**
* @author Marblog
*/
public class Main {
public static void main(String[] args) {
String str1 = "十八岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来的街坊邻居收到了是的连接数量的飞机螺丝钉解放了解开了方式简单快乐附件是家里的附件两款手机看分时";
String str2 = "邻居收到了是的连接数量的飞机螺丝钉解放了解开了方式简单快乐附件是家里的附件两款手机看分时间段开了房间昆仑山大家看了附近开了世界的分厘卡即使打开链接发可怜见可怜见了解离开家了就了解离开家可怜见可怜见了就离开家离开家了就了士大夫士大夫士大夫";
String str3 = "十七岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来十六岁老地方老时间的风景史蒂夫罗杰斯点击发送来的街坊邻居收到了是的连接数量的飞机螺丝钉解放了解开了方式简单快乐附件是家里的附件两款手机看分时";
System.out.println("len:" + str1.length());
System.out.println("len:" + str2.length());
System.out.println("len:" + str3.length());
String str = new StringBuffer().append(str1).append(str2).append(str3).append("...").toString();
System.out.println("message: " + str1.length() + "," + str2.length() + "," + str3.length());
String s1 = null;
int num = 100; //多个字符视为一包数据
int index = 0;
int lines = (str.length() + (num - 1)) / num;
int center = ((lines + 1) / 2);
System.out.println("字符串长度:" + str.length());
System.out.println("可以分为:" + lines + "行");
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i += num) {
index++;
System.out.print("中间行=" + center + "当前行:" + index);
s1 = String.valueOf(chars[i]);
for (int j = 1; j <= num - 1; j++) {
if (i + j < chars.length)
s1 = s1.concat(String.valueOf(chars[i + j]));
}
if (index == center) {
System.out.print("-->");
}
System.out.println(s1);
}
}
}
2、运行结果