短信长度判断:判断是长短信

判断思路:

先筛选出短信内容中包含的中文字符,再用短信内容长度减去中文字符,便得到剩下的字符数,然后算出总字节数

/**
* 判断是否是长短信
*/
private Integer judgeLongMsg(String tempContent) {
//判断短信中有几个中文字符
int count=0;
String regex = "[u4e00-u9fa5]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(tempContent);
while (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("短信内容中共有 " + count + "个汉字 ");
//判断是否是长短信
int msgContentLength=tempContent.length();
Integer otherChar=msgContentLength-count;//短信中除中文外有几个其他字符
Integer judgeMsgContent=count*2+otherChar;
Integer islongMsg;
if(judgeMsgContent<=140) {//最多发送140个字节
islongMsg=0;
}else {
islongMsg=1;
}
return islongMsg;
}

原文地址:https://www.cnblogs.com/curedfisher/p/12243472.html