@Test public void testSubString_01() { assertEquals("01", null, StringUtil.substring(null, 0, 0)); assertEquals("02", "", StringUtil.substring("This is test String", 0, 0)); assertEquals("03", "", StringUtil.substring("This is test String", -1, 0)); assertEquals("04", "", StringUtil.substring("This is test String", 19, 25)); assertEquals("05", "This is test Strin", StringUtil.substring("This is test String", 0, -1)); assertEquals("06", "", StringUtil.substring("This is test String", -1, -1)); assertEquals("07", "This is te", StringUtil.substring("This is test String", 0, 10)); assertEquals("08", "is te", StringUtil.substring("This is test String", 5, 10)); }
public static String substring(String string, int from, int to) { if (string == null) { return null; } if (from < 0) { from = Math.max(0, string.length() + from); } if (to < 0) { to = Math.max(0, string.length() + to); } else { to = Math.min(string.length(), to); } if (from > string.length() || to < from) { return EMPTY; } return string.substring(from, to); }
StringUtil { public static String substring(String string, int from, int to) { if (string == null) { return null; } if (from < 0) { from = Math.max(0, string.length() + from); } if (to < 0) { to = Math.max(0, string.length() + to); } else { to = Math.min(string.length(), to); } if (from > string.length() || to < from) { return EMPTY; } return string.substring(from, to); } }
StringUtil { public static String substring(String string, int from, int to) { if (string == null) { return null; } if (from < 0) { from = Math.max(0, string.length() + from); } if (to < 0) { to = Math.max(0, string.length() + to); } else { to = Math.min(string.length(), to); } if (from > string.length() || to < from) { return EMPTY; } return string.substring(from, to); }  }
StringUtil { public static String substring(String string, int from, int to) { if (string == null) { return null; } if (from < 0) { from = Math.max(0, string.length() + from); } if (to < 0) { to = Math.max(0, string.length() + to); } else { to = Math.min(string.length(), to); } if (from > string.length() || to < from) { return EMPTY; } return string.substring(from, to); }  static String substr(String string, int from); static String substr(String string, int from, int length); static String zpad(Object obj, int width, String separator); static String zpad(Object obj, int width); static String lpad(Object obj, int width, String separator); static String pad(Object obj, int width); static String pad(Object obj, int width, char padChar); static String pad(Object obj, int width, char padChar, String separator); static String lpad(Object obj, int width); static String lpad(Object obj, int width, char padChar); static String lpad(Object obj, int width, char padChar, String separator); static String rpad(Object obj, int width, String separator); static String rpad(Object obj, int width); static String rpad(Object obj, int width, char padChar); static String rpad(Object obj, int width, char padChar, String separator); static String substring(String string, int from, int to); static String toString(byte[] bytes); static String lchop(String string, String word); static String rchop(String string, String word); static String[] split(String s, String delim); static String[] split(String s, String delim, int numParts); static String trim(String s, char c); static String ltrim(String s, char c); static String trim(String s, char... chars); static String trim(String s, String chars); static String rtrim(String s, char c); static boolean isTrue(String s); static boolean isTrue(String s, boolean dfault); static String getResourceAsString(String path); static InputStream toInputStream(String s); static InputStream toInputStream(String s, String charset); static String fromInputStream(InputStream is); static String fromInputStream(InputStream is, String charset); static String fromInputStream(InputStream is, String charset, int bufsize); static int toInt(String s); static String emptyIfNull(String str); }
StringUtil { public static String substring(String string, int from, int to) { if (string == null) { return null; } if (from < 0) { from = Math.max(0, string.length() + from); } if (to < 0) { to = Math.max(0, string.length() + to); } else { to = Math.min(string.length(), to); } if (from > string.length() || to < from) { return EMPTY; } return string.substring(from, to); }  static String substr(String string, int from); static String substr(String string, int from, int length); static String zpad(Object obj, int width, String separator); static String zpad(Object obj, int width); static String lpad(Object obj, int width, String separator); static String pad(Object obj, int width); static String pad(Object obj, int width, char padChar); static String pad(Object obj, int width, char padChar, String separator); static String lpad(Object obj, int width); static String lpad(Object obj, int width, char padChar); static String lpad(Object obj, int width, char padChar, String separator); static String rpad(Object obj, int width, String separator); static String rpad(Object obj, int width); static String rpad(Object obj, int width, char padChar); static String rpad(Object obj, int width, char padChar, String separator); static String substring(String string, int from, int to); static String toString(byte[] bytes); static String lchop(String string, String word); static String rchop(String string, String word); static String[] split(String s, String delim); static String[] split(String s, String delim, int numParts); static String trim(String s, char c); static String ltrim(String s, char c); static String trim(String s, char... chars); static String trim(String s, String chars); static String rtrim(String s, char c); static boolean isTrue(String s); static boolean isTrue(String s, boolean dfault); static String getResourceAsString(String path); static InputStream toInputStream(String s); static InputStream toInputStream(String s, String charset); static String fromInputStream(InputStream is); static String fromInputStream(InputStream is, String charset); static String fromInputStream(InputStream is, String charset, int bufsize); static int toInt(String s); static String emptyIfNull(String str); static final String EMPTY; }