Limiting the number of characters in a string, and chopping off the rest

前端 未结 8 1193
故里飘歌
故里飘歌 2020-12-08 02:02

I need to create a summary table at the end of a log with some values that are obtained inside a class. The table needs to be printed in fixed-width format. I have the cod

8条回答
  •  再見小時候
    2020-12-08 02:28

    If you just want a maximum length, use StringUtils.left! No if or ternary ?: needed.

    int maxLength = 5;
    StringUtils.left(string, maxLength);
    

    Output:

          null -> null
            "" -> ""
           "a" -> "a"
    "abcd1234" -> "abcd1"
    

    Left Documentation

提交回复
热议问题