Java - Write code to truncate String by using substring

Requirements

Write code to truncate String

use ... to show that the string is shortened.

Hint

Check the string length and use substring to get the part you would keep.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String name = "book2s.combook2s.com book2s.com book2s.com book2s.com book2s.com book2s.com book2s.com book2s.com book2s.com";
        System.out.println(truncateString(name));
    }/*from  w w w. jav a  2 s  .  c  o  m*/

    private static final int TRUNCATE_LENGTH = 60;

    public static String truncateString(String name) {
        if (name != null && name.length() > TRUNCATE_LENGTH) {
            return name.substring(0, TRUNCATE_LENGTH) + " ...";
        }
        return name;
    }
}

Related Exercise