业务需求:现在需要分包发送给通信模块,每一条消息最多只能发送17个字节,所以必须把一个长字符串进行分段处理
代码区域:
/**
-
Created by wf on 2022/5/27 - 14:21
-
如果在分段点刚好是汉字,则前一段会小于设定阈值
**/
public class SpecialCharacterActivity extends AppCompatActivity {
private static final String TAG = “wufeng_Special”;
public static final int CODE_100 = 100;
private static final int DIV_NUM = 18;public static final String SPECIFIC_SYMBOL_Ψ = “Ψ”;//specific symbol Ψ
public static final String SPECIFIC_SYMBOL_卍 = “卍”;//specific symbol 卍
public static final String ARRAYSLIST = “42902324324231202这其中掺杂了汉字4646567552330110001&世界上任何事物都有裂痕,那是光透进来的地方!这是一个测试demo”;//数据源
private Handler mHandler = new SpecialHandler(this);
private String newAllData = “”;
private final class SpecialHandler extends Handler {
private final WeakReference<SpecialCharacterActivity> mActivity; public SpecialHandler(SpecialCharacterActivity activity) { mActivity = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { if (mActivity.get() == null) { return; } int what = msg.what; if (what == CODE_100) { String obj = (String) msg.obj; String before = substringBefore(obj, SPECIFIC_SYMBOL_Ψ); String after = substringAfter(obj, SPECIFIC_SYMBOL_Ψ); boolean state = chineseHaveByteBoolean(after, DIV_NUM); newAllData += before + SPECIFIC_SYMBOL_卍; Log.v(TAG, " 有效数据:" + before + " 有效字节状态: " + state + " 剩余数据: " + after); if (state) { SegmentString(after, DIV_NUM); } else { newAllData += after; String[] ssss = newAllData.split(SPECIFIC_SYMBOL_卍); Log.v(TAG, " 完成:" + after + " 有效字节状态: " + state + " \n"); for (int i = 0; i < ssss.length; i++) { Log.v(TAG, "第" + i + "条数据:" + ssss[i] + " 有多少个字节:" + chineseHaveByte(ssss[i])); } } } }
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_special_character);
SegmentString(ARRAYSLIST, DIV_NUM);
}@Override
protected void onDestroy() {
super.onDestroy();
if (mHandler!=null)mHandler.removeCallbacksAndMessages(null);
}private void SegmentString(String str, int divNum) {
Pattern pattern = Pattern.compile(“[\u4e00-\u9fa5]”);
char c[] = str.toCharArray();
int count = 0;//字节个数
int oldCountMod = 0;for (int j = 0; j < str.length(); j++) { Matcher matcher = pattern.matcher(String.valueOf(c[j])); if (matcher.matches()) { count += 2; } else { count++; } if (count >= divNum) { sendMeaageToSecial(str.substring(0, j) + SPECIFIC_SYMBOL_Ψ + str.substring(j)); break; } }
}
private void sendMeaageToSecial(String str) {
Message message = Message.obtain();
message.what = CODE_100;
message.obj = str;
mHandler.sendMessage(message);
}/**
*- @param str
- @param separator
- @return
*/
public static String substringBefore(String str, String separator) {
if (!isEmpty(str) && separator != null) {
if (separator.length() == 0) {
return “”;
} else {
int pos = str.indexOf(separator);
return pos == -1 ? str : str.substring(0, pos);
}
} else {
return str;
}
}
public static String substringAfter(String str, String separator) {
if (isEmpty(str)) {
return str;
} else if (separator == null) {
return “”;
} else {
int pos = str.indexOf(separator);
return pos == -1 ? “” : str.substring(pos + separator.length());
}
}/**
-
字符串中有多少个字节
-
@param str
-
@return
*/
public static int chineseHaveByte(String str) {
Pattern pattern = Pattern.compile(“[\u4e00-\u9fa5]”);
char c[] = str.toCharArray();
int count = 0;
for (int i = 0; i < str.length(); i++) {
Matcher matcher = pattern.matcher(String.valueOf(c[i]));
if (matcher.matches()) {
count += 2;
} else {
count++;
}}
return count;
}
/**
- 判断字符串是否达到设置的阈值
- @param str
- @param setNum
*/
public static boolean chineseHaveByteBoolean(String str, int setNum) {
Pattern pattern = Pattern.compile(“[\u4e00-\u9fa5]”);
char c[] = str.toCharArray();
int count = 0;
for (int i = 0; i < str.length(); i++) {
Matcher matcher = pattern.matcher(String.valueOf(c[i]));
if (matcher.matches()) {
count += 2;
} else {
count++;
}
if (count >= setNum) {
return true;
}
}
return false;
}
}
打印日志如下图: