Java - Write code to cut string by length using if statement

Requirements

Write code to cut string by length using if statement

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        int maxLen = 42;
        System.out.println(subStr(str, maxLen));
    }/*from ww  w . j a  va  2s  .c  om*/

    public static String subStr(String str, int maxLen) {
        if (str == null) {
            return null;
        }
        if (maxLen < 1) {
            return str;
        }
        if (str.length() > maxLen) {
            return str.substring(0, maxLen) + "...";
        }
        return str;
    }
}

Related Exercise