@Test public void testTruncate() { String[] src = { null, "", "123", "abc", "ABC", "中华人", "12345", "abcde", "ABCDE", "中华人民共", "1234567890", "abcdefghij", "ABCDEFGHIJK", "中华人民共和国万岁！" }; String[] tar = { null, "", "123", "abc", "ABC", "中华人", "12345", "abcde", "ABCDE", "中华人民共", "12345...", "abcde...", "ABCDE...", "中华人民共..." }; for (int index =0; index < src.length; index ++) { assertEquals(tar[index], StringUtils.truncate(src[index], 5)); } }
public static String truncate(String src, int length) { if (src == null || src.length() <= length) { return src; } return String.format("%s...", src.substring(0, length)); }
StringUtils { public static String truncate(String src, int length) { if (src == null || src.length() <= length) { return src; } return String.format("%s...", src.substring(0, length)); } }
StringUtils { public static String truncate(String src, int length) { if (src == null || src.length() <= length) { return src; } return String.format("%s...", src.substring(0, length)); } private  StringUtils(); }
StringUtils { public static String truncate(String src, int length) { if (src == null || src.length() <= length) { return src; } return String.format("%s...", src.substring(0, length)); } private  StringUtils(); static String byte2Base64String(byte[] value); static byte[] Base64String2Byte(String base64Str); static String byte2HexString(byte b); static byte[] hexString2Byte(String input); static String byte2HexString(byte[] bs); static boolean isBlank(String str); static boolean isChinese(String str); static boolean isChinese(char c); static String merge(Collection<?> coll); static String merge(Collection<?> coll, char separate); static String merge(Collection<?> coll, String separate); static String merge(String[] str); static String merge(String[] str, char separate); static String merge(String[] str, String separate); static String merge(String str, int length); static String merge(String str, int length, String separate); static String merge(String str, int length, char separate); static String repeat(int length, String str); static String[] split(String s); static String[] split(String s, boolean trimTokens, boolean ignoreEmptyTokens); static String[] split(String s, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens); static boolean string2Boolean(String str, boolean defaultValue); static int string2Int(String str, TypeUtils.Radix radix, int defaultValue); static long string2Long(String str, TypeUtils.Radix radix, long defaultValue); static float string2Float(String str, float defaultValue); static double string2Double(String str, double defaultValue); static long string2Size(String size, long defaultValue); static long stirng2TimePeriod(String timePeriod, long defaultValue); static String truncate(String src, int length); static String xmlEscape(String str); static String xmlUnescape(String str); }
StringUtils { public static String truncate(String src, int length) { if (src == null || src.length() <= length) { return src; } return String.format("%s...", src.substring(0, length)); } private  StringUtils(); static String byte2Base64String(byte[] value); static byte[] Base64String2Byte(String base64Str); static String byte2HexString(byte b); static byte[] hexString2Byte(String input); static String byte2HexString(byte[] bs); static boolean isBlank(String str); static boolean isChinese(String str); static boolean isChinese(char c); static String merge(Collection<?> coll); static String merge(Collection<?> coll, char separate); static String merge(Collection<?> coll, String separate); static String merge(String[] str); static String merge(String[] str, char separate); static String merge(String[] str, String separate); static String merge(String str, int length); static String merge(String str, int length, String separate); static String merge(String str, int length, char separate); static String repeat(int length, String str); static String[] split(String s); static String[] split(String s, boolean trimTokens, boolean ignoreEmptyTokens); static String[] split(String s, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens); static boolean string2Boolean(String str, boolean defaultValue); static int string2Int(String str, TypeUtils.Radix radix, int defaultValue); static long string2Long(String str, TypeUtils.Radix radix, long defaultValue); static float string2Float(String str, float defaultValue); static double string2Double(String str, double defaultValue); static long string2Size(String size, long defaultValue); static long stirng2TimePeriod(String timePeriod, long defaultValue); static String truncate(String src, int length); static String xmlEscape(String str); static String xmlUnescape(String str); static final char DEFAULT_SEPARATOR; static final String DELIMITERS; }