将任意一个字符串分割成n段,每一段的字节长度必须小于设置的阈值

业务需求:现在需要分包发送给通信模块,每一条消息最多只能发送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;
      }

}
打印日志如下图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值