@Test public void insertFromRightTest() { final String s = "0123456789"; assertEquals(insertFromRight(s, 0, "-"), "0123456789"); assertEquals(insertFromRight(s, 1, "-"), "0-1-2-3-4-5-6-7-8-9"); assertEquals(insertFromRight(s, 2, "-"), "01-23-45-67-89"); assertEquals(insertFromRight(s, 3, "-"), "0-123-456-789"); assertEquals(insertFromRight(s, 5, "-"), "01234-56789"); assertEquals(insertFromRight(s, 6, "-"), "0123-456789"); assertEquals(insertFromRight(s, 9, "-"), "0-123456789"); assertEquals(insertFromRight(s, 10, "-"), "0123456789"); assertEquals(insertFromRight(s, 12, "-"), "0123456789"); assertEquals(insertFromRight(s, 2, "--"), "01--23--45--67--89"); assertEquals(insertFromRight(s, 3, "--"), "0--123--456--789"); }
public static String insertFromRight(String str, int interval, String stringToInsert) { if (interval < 0) { throw new IllegalArgumentException("interval value cannot be negative."); } if (str == null || interval == 0 || interval >= str.length() || isNullOrEmpty(stringToInsert)) { return str; } StringBuilder b = new StringBuilder(); int j = 0; for (int i = str.length() - 1; i >= 0; i--) { b.append(str.charAt(i)); j++; if (j % interval == 0 && j <= str.length() - 1) { b.append(stringToInsert); } } return reverse(b.toString()); }
Strings { public static String insertFromRight(String str, int interval, String stringToInsert) { if (interval < 0) { throw new IllegalArgumentException("interval value cannot be negative."); } if (str == null || interval == 0 || interval >= str.length() || isNullOrEmpty(stringToInsert)) { return str; } StringBuilder b = new StringBuilder(); int j = 0; for (int i = str.length() - 1; i >= 0; i--) { b.append(str.charAt(i)); j++; if (j % interval == 0 && j <= str.length() - 1) { b.append(stringToInsert); } } return reverse(b.toString()); } }
Strings { public static String insertFromRight(String str, int interval, String stringToInsert) { if (interval < 0) { throw new IllegalArgumentException("interval value cannot be negative."); } if (str == null || interval == 0 || interval >= str.length() || isNullOrEmpty(stringToInsert)) { return str; } StringBuilder b = new StringBuilder(); int j = 0; for (int i = str.length() - 1; i >= 0; i--) { b.append(str.charAt(i)); j++; if (j % interval == 0 && j <= str.length() - 1) { b.append(stringToInsert); } } return reverse(b.toString()); } private  Strings(); }
Strings { public static String insertFromRight(String str, int interval, String stringToInsert) { if (interval < 0) { throw new IllegalArgumentException("interval value cannot be negative."); } if (str == null || interval == 0 || interval >= str.length() || isNullOrEmpty(stringToInsert)) { return str; } StringBuilder b = new StringBuilder(); int j = 0; for (int i = str.length() - 1; i >= 0; i--) { b.append(str.charAt(i)); j++; if (j % interval == 0 && j <= str.length() - 1) { b.append(stringToInsert); } } return reverse(b.toString()); } private  Strings(); static boolean isNullOrEmpty(String str); static boolean hasText(String s); static boolean allHasText(String... strings); static boolean allNullOrEmpty(String... strings); static String leftTrim(String s); static String rightTrim(String str); static boolean containsNone(String str, String invalidCharsStr); static boolean containsOnly(String str, String allowedChars); static String repeat(char c, int count); static String repeat(String str, int count); static String reverse(String str); static String insertFromLeft(String str, int interval, String stringToInsert); static String insertFromRight(String str, int interval, String stringToInsert); static String rightPad(String str, int size); static String rightPad(String str, int size, char padChar); static String rightPad(String str, int size, String padStr); static String leftPad(String str, int size); static String leftPad(String str, int size, char padChar); static String subStringUntilFirst(String str, String s); static String subStringUntilLast(String str, String s); static String subStringAfterFirst(String str, String s); static String subStringAfterLast(String str, String s); static String leftPad(String str, int size, String padStr); static String whiteSpacesToSingleSpace(String str); static String eliminateWhiteSpaces(String str); static String[] separateGrams(String word, int gramSize); }
Strings { public static String insertFromRight(String str, int interval, String stringToInsert) { if (interval < 0) { throw new IllegalArgumentException("interval value cannot be negative."); } if (str == null || interval == 0 || interval >= str.length() || isNullOrEmpty(stringToInsert)) { return str; } StringBuilder b = new StringBuilder(); int j = 0; for (int i = str.length() - 1; i >= 0; i--) { b.append(str.charAt(i)); j++; if (j % interval == 0 && j <= str.length() - 1) { b.append(stringToInsert); } } return reverse(b.toString()); } private  Strings(); static boolean isNullOrEmpty(String str); static boolean hasText(String s); static boolean allHasText(String... strings); static boolean allNullOrEmpty(String... strings); static String leftTrim(String s); static String rightTrim(String str); static boolean containsNone(String str, String invalidCharsStr); static boolean containsOnly(String str, String allowedChars); static String repeat(char c, int count); static String repeat(String str, int count); static String reverse(String str); static String insertFromLeft(String str, int interval, String stringToInsert); static String insertFromRight(String str, int interval, String stringToInsert); static String rightPad(String str, int size); static String rightPad(String str, int size, char padChar); static String rightPad(String str, int size, String padStr); static String leftPad(String str, int size); static String leftPad(String str, int size, char padChar); static String subStringUntilFirst(String str, String s); static String subStringUntilLast(String str, String s); static String subStringAfterFirst(String str, String s); static String subStringAfterLast(String str, String s); static String leftPad(String str, int size, String padStr); static String whiteSpacesToSingleSpace(String str); static String eliminateWhiteSpaces(String str); static String[] separateGrams(String word, int gramSize); static final String EMPTY_STRING; static final String[] EMPTY_STRING_ARRAY; }