Java - Write code to ellipsize String by length

Requirements

Write code to ellipsize String

Check null value, check the specified length and string length

Hint

Use String.substring

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        int limit = 4;
        System.out.println(ellipsizeString(string, limit));
    }/*from   w w  w  .  ja v  a  2 s. c o m*/

    public static String ellipsizeString(String string, int limit) {
        String retValue = "";
        if (string != null) {
            if (string.length() > limit) {
                retValue = string.substring(0, limit) + "...";
            } else {
                retValue = string;
            }
        }
        return retValue;
    }
}

Related Exercise