Java - Write code to trim String by length

Requirements

Write code to trim String by length

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        int length = 2;
        System.out.println(trimString(str, length));
    }/*from   w ww.  ja  va2 s .c  om*/

    public static String trimString(String str, int length) {
        if (str == null) {
            return "";
        } else if (str.length() > length) {
            return str.substring(0, length - 3) + "...";
        } else {
            return str;
        }
    }
}