Trim a string based on the string length

后端 未结 12 862
無奈伤痛
無奈伤痛 2020-12-12 14:29

I want to trim a string if the length exceeds 10 characters.

Suppose if the string length is 12 (String s=\"abcdafghijkl\"), then the new trimmed string

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 15:02

    As usual nobody cares about UTF-16 surrogate pairs. See about them: What are the most common non-BMP Unicode characters in actual use? Even authors of org.apache.commons/commons-lang3

    You can see difference between correct code and usual code in this sample:

    public static void main(String[] args) {
        //string with FACE WITH TEARS OF JOY symbol
        String s = "abcdafghi\uD83D\uDE02cdefg";
        int maxWidth = 10;
        System.out.println(s);
        //do not care about UTF-16 surrogate pairs
        System.out.println(s.substring(0, Math.min(s.length(), maxWidth)));
        //correctly process UTF-16 surrogate pairs
        if(s.length()>maxWidth){
            int correctedMaxWidth = (Character.isLowSurrogate(s.charAt(maxWidth)))&&maxWidth>0 ? maxWidth-1 : maxWidth;
            System.out.println(s.substring(0, Math.min(s.length(), correctedMaxWidth)));
        }
    }
    

提交回复
热议问题