/**
* 以给定的 LENGTH 分割短信内容,返回分割后的字符串数组
* 以下是返回内容示例:
*
*
* sSMS = “abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz”;
* 1. 分割后的短信个数 < 10
* LENGTH = 10;
* (1/6)abcdefghij
* (2/6)klmnopqrst
* (3/6)uvwxyzabcd
* (4/6)efghijklmn
* (5/6)opqrstuvwx
* (6/6)yz = 7
*
* 2. 分割后的短信个数 >= 10
* LENGTH = 5;
* (1/15)abcd
* (2/15)efgh
* (3/15)ijkl
* ......
* (13/15)tuv
* (14/15)wxy
* (15/15)z
*
*
* @param sSMS
* 短信内容
* @param LENGTH
* 每条短信的长度
* @return 一组短信内容
*/
public static String[] smsSplit(String sSMS, final int LENGTH) {
int iContentLength = sSMS.trim().length();
if (LENGTH >= iContentLength) {
// 短信内容的长度小于等于LENGTH,不用分割直接返回
return new String[] { sSMS };
}
final int iSMS_Num_9 = 9; // 分割9条短信
int iSum = LENGTH * iSMS_Num_9; // 短信条数等于9的总长度,
int iLength = 0;
if (iContentLength > iSum) {
// 源短信的长度大于iSum,表示源短信可以被分割的数量超过10条
iLength = LENGTH - 1;
iSum = iLength * iSMS_Num_9;
} else if (iContentLength < iSum) {
// 源短信的长度小于iSum,表示源短信可以被分割的数量小于等于9条
iLength = LENGTH;
iSum = iContentLength;
}
// 短信个数小于10
String sSMS9 = sSMS.trim().substring(0, iSum);
String[] aSMS9 = smsSplitByLength(sSMS9, iLength);
if (iContentLength == iSum) {
List
String sTemp;
final int iSMSNum = aSMS9.length;
for (int i = 0; i < aSMS9.length; i++) {
sTemp = "(" + (i + 1) + "/" + iSMSNum + ")" + aSMS9[i];
oList.add(sTemp);
}
String[] aSMS = oList.toArray(new String[0]);
return aSMS;
}
String sTemp1 = aSMS9[aSMS9.length - 1];
String sTemp2 = aSMS9[aSMS9.length - 2];
String sSMS10 = null;
String[] aSMS10 = null;
if (sTemp1.length() != sTemp2.length()) {
String[] aTemp = aSMS9;
aSMS9 = new String[aTemp.length - 1];
System.arraycopy(aTemp, 0, aSMS9, 0, aTemp.length - 1);
sSMS10 = sTemp1 + sSMS.trim().substring(iSum);
aSMS10 = smsSplitByLength(sSMS10, iLength - 1);
} else {
sSMS10 = sSMS.trim().substring(iSum);
aSMS10 = smsSplitByLength(sSMS10, iLength - 1);
}
List
String sTemp;
final int iSMSNum = aSMS9.length + aSMS10.length;
for (int i = 0; i < aSMS9.length; i++) {
sTemp = "(" + (i + 1) + "/" + iSMSNum + ")" + aSMS9[i];
oList.add(sTemp);
}
for (int i = 0; i < (aSMS10.length); i++) {
sTemp = "(" + (i + 10) + "/" + iSMSNum + ")" + aSMS10[i];
oList.add(sTemp);
}
String[] aSMS = oList.toArray(new String[0]);
return aSMS;
}
/**
* 以给定的 LENGTH 分割短信内容,返回分割后的字符串数组
* 注意:返回数组中的最后一个元素的字符串长度不一定等于 LENGTH ,
* 即不考虑 sSMS.length % LENGTH != 0
*
* @param sSMS
* 短信内容
* @param LENGTH
* 每条短信的长度
* @return 一组短信内容
*/
private static String[] smsSplitByLength(String sSMS, final int LENGTH) {
int iContentLength = sSMS.trim().length();
String sTemp;
int x = 0;
int y = 0;
List
while (true) {
y = x + LENGTH;
if (y > iContentLength)
y = iContentLength;
sTemp = sSMS.substring(x, y);
l.add(sTemp);
x = y;
if (x == iContentLength)
break;
}
return l.toArray(new String[0]);
}