`
hollowinhart
  • 浏览: 124851 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

按字节截取一定字符

 
阅读更多

public static void main(String[] args) throws UnsupportedEncodingException
    {
        String info ="123中国人ABC";
        info = truncByByte(info,5);
        System.out.println(info);
    }
   
    private static String truncByByte(String info,int len) throws UnsupportedEncodingException
    {
        byte[] arrs = info.getBytes("GBK");
        int count = 0;
       
        boolean isChineseFirst = false;
       
        for(int i=0;i<len;i++)
        {
            System.out.println(arrs[i]);
            // 是汉字
            if(arrs[i]<0 && !isChineseFirst)
            {
                isChineseFirst = true;
            }
            else
            {
                isChineseFirst = false;
                count ++;
            }
        }
       
        return info.substring(0, count);
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics