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));
    }/* w w w.j a v  a2  s  .  c  o m*/

    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