@Test public void testCompressMiddle() throws Exception { assertEquals(StrUtil.compressMiddle("0123456789", 2, 2), "01...89"); assertEquals(StrUtil.compressMiddle("0123456789", 5, 5), "0123456789"); assertEquals(StrUtil.compressMiddle("0123456789", 6, 7), "0123456789"); assertEquals(StrUtil.compressMiddle("0123456789", 10, 1), "0123456789"); assertEquals(StrUtil.compressMiddle("0123456789", 20, 2), "0123456789"); assertEquals(StrUtil.compressMiddle("0123456789", 2, 20), "0123456789"); }
public static String compressMiddle(String s, int headLength, int tailLength) { Validate.notNull(s, "Input string must not be null"); Validate.isTrue(headLength > 0, "Head length must be larger than 0"); Validate.isTrue(tailLength > 0, "Tail length must be larger than 0"); if(headLength + tailLength >= s.length()) { return s; } return s.substring(0, headLength) + "..." + s.substring(s.length() - tailLength); }
StrUtil { public static String compressMiddle(String s, int headLength, int tailLength) { Validate.notNull(s, "Input string must not be null"); Validate.isTrue(headLength > 0, "Head length must be larger than 0"); Validate.isTrue(tailLength > 0, "Tail length must be larger than 0"); if(headLength + tailLength >= s.length()) { return s; } return s.substring(0, headLength) + "..." + s.substring(s.length() - tailLength); } }
StrUtil { public static String compressMiddle(String s, int headLength, int tailLength) { Validate.notNull(s, "Input string must not be null"); Validate.isTrue(headLength > 0, "Head length must be larger than 0"); Validate.isTrue(tailLength > 0, "Tail length must be larger than 0"); if(headLength + tailLength >= s.length()) { return s; } return s.substring(0, headLength) + "..." + s.substring(s.length() - tailLength); } private  StrUtil(); }
StrUtil { public static String compressMiddle(String s, int headLength, int tailLength) { Validate.notNull(s, "Input string must not be null"); Validate.isTrue(headLength > 0, "Head length must be larger than 0"); Validate.isTrue(tailLength > 0, "Tail length must be larger than 0"); if(headLength + tailLength >= s.length()) { return s; } return s.substring(0, headLength) + "..." + s.substring(s.length() - tailLength); } private  StrUtil(); static String stringify(long byteNumber); static String replaceVariable(final String param); static String compressMiddle(String s, int headLength, int tailLength); }
StrUtil { public static String compressMiddle(String s, int headLength, int tailLength) { Validate.notNull(s, "Input string must not be null"); Validate.isTrue(headLength > 0, "Head length must be larger than 0"); Validate.isTrue(tailLength > 0, "Tail length must be larger than 0"); if(headLength + tailLength >= s.length()) { return s; } return s.substring(0, headLength) + "..." + s.substring(s.length() - tailLength); } private  StrUtil(); static String stringify(long byteNumber); static String replaceVariable(final String param); static String compressMiddle(String s, int headLength, int tailLength);  }