全角半角混在截取固定位数

这篇博客探讨了如何在Java中处理全角和半角字符混合的情况,实现了根据字符长度而非字节长度进行截取的方法。通过示例代码展示了不同起始位置的截取结果。
摘要由CSDN通过智能技术生成

package zkktest;

import java.io.UnsupportedEncodingException;


public class test
{

    public static void main(String[] args) {
        String inStr = "新2鮮";
        System.out.println("old Str:" + inStr + "," + inStr.getBytes().length);
        System.out.println("0 Str:" + substringByCount(inStr, 0));
        System.out.println("1 Str:" + substringByCount(inStr, 1));
        System.out.println("2 Str:" + substringByCount(inStr, 2));
        System.out.println("3 Str:" + substringByCount(inStr, 3));
        System.out.println("4 Str:" + substringByCount(inStr, 4));
        System.out.println("5 Str:" + substringByCount(inStr, 5));
        System.out.println("6 Str:" + substringByCount(inStr, 6));
        System.out.println("7 Str:" + substringByCount(inStr, 7));
        System.out.println("8 Str:" + substringByCount(inStr, 8));
       
        String inStr1 = "2新鮮";
        System.out.println("old Str:" + inStr1 + "," + inStr1.getBytes().length);
        System.out.println("0 Str:" + substringByCount(inStr1, 0));
        System.out.println("1 Str:" + substringByCount(inStr1, 1));
        System.out.println("2 Str:" + substringByCount(inStr1, 2));
        System.out.println("3 Str:" + substringByCount(inStr1, 3));
        System.out.println("4 Str:" + substringByCount(inStr1, 4));
        System.out.println("5 Str:" + substringByCount(inStr1, 5));
        System.out.println("6 Str:" + substringByCount(inStr1, 6));
        System.out.println("7 Str:" + substringByCount(inStr1, 7));
        System.out.println("8 Str:" + substringByCount(inStr1, 8));
       
        String inStr2 = "新鮮2";
        System.out.println("old Str:" + inStr2 + "," + inStr2.getBytes().length);
        System.out.println("0 Str:" + substringByCount(inStr2, 0));
        System.out.println("1 Str:" + substringByCount(inStr2, 1));
        System.out.println("2 Str:" + substringByCount(inStr2, 2));
        System.out.println("3 Str:" + substringByCount(inStr2, 3));
        System.out.println("4 Str:" + substringByCount(inStr2, 4));
        System.out.println("5 Str:" + substringByCount(inStr2, 5));
        System.out.println("6 Str:" + substringByCount(inStr2, 6));
        System.out.println("7 Str:" + substringByCount(inStr2, 7));
        System.out.println("8 Str:" + substringByCount(inStr2, 8));
        
    }


    private static String substringByCount(String str, int count) {
        StringBuffer buff = new StringBuffer();
        if (StringUtils.isNotEmpty(str)) {
            if (count > 0) {
                char c;
                int sumCount = 0;
                for (int i = 0; i < str.length(); i++) {
                    c = str.charAt(i);
                    sumCount += String.valueOf(c).getBytes(Charset.forName('UTF-8')).length;
                    if (sumCount > count) {
                        break;
                    }
                    buff.append(c);
                }
            }
        }
        return buff.toString();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值