Automatically ellipsize a string in Java

前端 未结 5 1958
深忆病人
深忆病人 2020-12-17 01:55

Is there a method in Java to automatically ellipsize a string? Just in Java, not other libraries.

Thanks.

5条回答
  •  無奈伤痛
    2020-12-17 02:33

    Sure, try this one:

    public static String ellipsise (String input, int maxLen) {
        if (input == null)
            return null;
        if ((input.length() < maxLen) || (maxLen < 3))
            return input;
        return input.substring (0, maxLen - 3) + "...";
    }
    

    This has the advantage of fixing the bug where the King's English is not used properly :-)

提交回复
热议问题