Here you can find the source of truncate(String string, final int length)
public static String truncate(String string, final int length)
//package com.java2s; //License from project: Open Source License public class Main { public static String truncate(String string, final int length) { if (string.length() > length) { string = string.substring(0, length); }//from w ww .ja va 2 s . c o m return string; } public static String substring(final String string, int fromIndex, int toIndex) { final int len = string.length(); if (fromIndex < 0) { fromIndex += len; if (toIndex == 0) { toIndex = len; } } if (toIndex < 0) { toIndex += len; } if (fromIndex < 0) { fromIndex = 0; } if (toIndex > len) { toIndex = len; } if (fromIndex >= toIndex) { return ""; } return string.substring(fromIndex, toIndex); } }