Automatically ellipsize a string in Java

前端 未结 5 1962
深忆病人
深忆病人 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:41

    There is not. But here's another crack at a simple method to do this.

    String ellipsize(String input, int maxLength) {
      if (input == null || input.length() < maxLength) {
        return input;
      }
      return input.substring(0, maxLength) + "...";
    }
    

提交回复
热议问题