Automatically ellipsize a string in Java

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

    A oneliner which of course does not outperform the other offered solutions:

    longString.replaceFirst("(.{5}).+(.{5})", "$1...$2")
    

    Will leave only first and last 5 characters and put ... in the middle.

提交回复
热议问题