欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

oss

程序员文章站 2022-07-12 23:38:00
...
public String truncStrBySize(String source)
    {
        if (source == null)
            return null;
        int start, stop, byteLen;
        int alen = source.getBytes().length;

        if (len > 0)
        {
            if (alen <= len)
                return source;
            start = stop = byteLen = 0;
            while (byteLen <= len && stop < source.length())
            {
                if (source.substring(stop, stop + 1).getBytes().length == 1)
                {
                    byteLen += 1;
                }
                else
                {
                    byteLen += 2;
                }
                stop++;
            }
            StringBuffer sb = new StringBuffer(source.substring(start, stop - 1));
            if (alen > len)
                sb.append(delim);
            return sb.toString();
        }
        start = (len < -alen ? 0 : alen + len);
        stop = alen;
        StringBuffer sb = new StringBuffer(source.substring(start / 2, stop / 2));
        if (-alen <= len)
            sb.insert(0, delim);
        return sb.toString();
    }
相关标签: stop