Cut Java String at a number of character

后端 未结 8 1491
我寻月下人不归
我寻月下人不归 2020-12-18 18:43

I would like to cut a Java String when this String length is > 50, and add \"...\" at the end of the string.

Example :

I have the f

相关标签:
8条回答
  • 2020-12-18 19:08

    Jakarta Commons StringUtils.abbreviate(). If, for some reason you don't want to use a 3rd-party library, at least copy the source code.

    One big benefit of this over the other answers to date is that it won't throw if you pass in a null.

    0 讨论(0)
  • 2020-12-18 19:09

    StringUtils.abbreviate("abcdefg", 6);

    This will give you the following result: abc...

    Where 6 is the needed length, and "abcdefg" is the string that needs to be abbrevieted.

    0 讨论(0)
提交回复
热议问题