Cut Java String at a number of character

java, string

Solution

Use substring and concatenate:

if(str.length() > 50)
    strOut = str.substring(0,7) + "...";

Problem

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 following Java String : ``` String str = "abcdefghijklmnopqrtuvwxyz"; ``` I would like to cut the String at length = 8 : Result must be: ``` String strOut = "abcdefgh..." ```

Original source