Write code to truncate string to a particular length
public class Main { public static void main(String[] argv) { String string = "book2s.com"; int length = 4; System.out.println(truncatString(string, length)); }/*from w w w . j a v a2s. c om*/ /** * This method truncates string to a particular length * * @param string * @param length * size to which new string required * @return truncated string */ public static String truncatString(String string, int length) { if (string == null) return ""; if (string.length() < length) return string; return string.substring(0, length); } }