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));
    }/* ww  w .j  a v a  2 s  .co m*/

    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;
        }
    }
}