Truncating Strings by Bytes

后端 未结 13 1700
醉酒成梦
醉酒成梦 2021-02-06 04:21

I create the following for truncating a string in java to a new string with a given number of bytes.

        String truncatedValue = \"\";
        String curren         


        
13条回答
  •  情歌与酒
    2021-02-06 04:28

    I've improved upon Peter Lawrey's solution to accurately handle surrogate pairs. In addition, I optimized based on the fact that the maximum number of bytes per char in UTF-8 encoding is 3.

    public static String substring(String text, int maxBytes) {
        for (int i = 0, len = text.length(); (len - i) * 3 > maxBytes;) {
            int j = text.offsetByCodePoints(i, 1);
            if ((maxBytes -= text.substring(i, j).getBytes(StandardCharsets.UTF_8).length) < 0)  
                return text.substring(0, i);
            i = j;
        }
        return text;
    }
    

提交回复
热议问题