Java - Write code to shorten a string by length and add ...

Requirements

Write code to shorten a string by length and add ...

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        int maxSize = 4;
        System.out.println(shorten(string, maxSize));
    }/*  ww  w .j  a v a  2s  .co m*/

    public static String shorten(String string, int maxSize) {
        String res;
        if (string.length() < maxSize) {
            res = string;
        } else {
            res = string.substring(0, maxSize) + "...";
        }
        return res;
    }
}