Java tutorial
//package com.java2s; public class Main { public static String subStr(String input, int len) { if (isEmptyOrNull(input)) { return null; } int inpuLen = input.length(); if (len >= inpuLen) { return input; } else { return input.substring(0, len); } } public static boolean isEmptyOrNull(String string) { if (string == null || string.trim().length() == 0 || string.equalsIgnoreCase("null")) { return true; } else { return false; } } }