@Test public void testAdvanceLastNumber3() { String advanced = TextTools.advanceLastNumber("1"); Assert.assertEquals("2", advanced); }
public static String advanceLastNumber(String originatingName) { if (originatingName.length() == 0) { return "1"; } int lastDigitChar = -1; int firstDigitChar = -1; for (int i = originatingName.length() - 1; i >= 0; i--) { char c = originatingName.charAt(i); if (lastDigitChar == -1 && Character.isDigit(c)) { lastDigitChar = i; } if (Character.isDigit(c)) { firstDigitChar = i; } if (! Character.isDigit(c) && firstDigitChar > -1) { break; } } if (lastDigitChar == -1) { return originatingName + "1"; } else { String oldDigitString = originatingName.substring(firstDigitChar, lastDigitChar + 1); Integer oldValue = Integer.parseInt(oldDigitString); Integer newValue = oldValue + 1; String newDigitString = newValue.toString(); int lengthDifference = oldDigitString.length() - newDigitString.length(); if (lengthDifference > 0) { String zeroPadding = new String(new char[lengthDifference]).replace("\0", "0"); newDigitString = zeroPadding + newDigitString; } String firstPart = originatingName.substring(0, firstDigitChar); String lastPart = originatingName.substring(lastDigitChar + 1); return firstPart + newDigitString + lastPart; } }
TextTools { public static String advanceLastNumber(String originatingName) { if (originatingName.length() == 0) { return "1"; } int lastDigitChar = -1; int firstDigitChar = -1; for (int i = originatingName.length() - 1; i >= 0; i--) { char c = originatingName.charAt(i); if (lastDigitChar == -1 && Character.isDigit(c)) { lastDigitChar = i; } if (Character.isDigit(c)) { firstDigitChar = i; } if (! Character.isDigit(c) && firstDigitChar > -1) { break; } } if (lastDigitChar == -1) { return originatingName + "1"; } else { String oldDigitString = originatingName.substring(firstDigitChar, lastDigitChar + 1); Integer oldValue = Integer.parseInt(oldDigitString); Integer newValue = oldValue + 1; String newDigitString = newValue.toString(); int lengthDifference = oldDigitString.length() - newDigitString.length(); if (lengthDifference > 0) { String zeroPadding = new String(new char[lengthDifference]).replace("\0", "0"); newDigitString = zeroPadding + newDigitString; } String firstPart = originatingName.substring(0, firstDigitChar); String lastPart = originatingName.substring(lastDigitChar + 1); return firstPart + newDigitString + lastPart; } } }
TextTools { public static String advanceLastNumber(String originatingName) { if (originatingName.length() == 0) { return "1"; } int lastDigitChar = -1; int firstDigitChar = -1; for (int i = originatingName.length() - 1; i >= 0; i--) { char c = originatingName.charAt(i); if (lastDigitChar == -1 && Character.isDigit(c)) { lastDigitChar = i; } if (Character.isDigit(c)) { firstDigitChar = i; } if (! Character.isDigit(c) && firstDigitChar > -1) { break; } } if (lastDigitChar == -1) { return originatingName + "1"; } else { String oldDigitString = originatingName.substring(firstDigitChar, lastDigitChar + 1); Integer oldValue = Integer.parseInt(oldDigitString); Integer newValue = oldValue + 1; String newDigitString = newValue.toString(); int lengthDifference = oldDigitString.length() - newDigitString.length(); if (lengthDifference > 0) { String zeroPadding = new String(new char[lengthDifference]).replace("\0", "0"); newDigitString = zeroPadding + newDigitString; } String firstPart = originatingName.substring(0, firstDigitChar); String lastPart = originatingName.substring(lastDigitChar + 1); return firstPart + newDigitString + lastPart; } }  }
TextTools { public static String advanceLastNumber(String originatingName) { if (originatingName.length() == 0) { return "1"; } int lastDigitChar = -1; int firstDigitChar = -1; for (int i = originatingName.length() - 1; i >= 0; i--) { char c = originatingName.charAt(i); if (lastDigitChar == -1 && Character.isDigit(c)) { lastDigitChar = i; } if (Character.isDigit(c)) { firstDigitChar = i; } if (! Character.isDigit(c) && firstDigitChar > -1) { break; } } if (lastDigitChar == -1) { return originatingName + "1"; } else { String oldDigitString = originatingName.substring(firstDigitChar, lastDigitChar + 1); Integer oldValue = Integer.parseInt(oldDigitString); Integer newValue = oldValue + 1; String newDigitString = newValue.toString(); int lengthDifference = oldDigitString.length() - newDigitString.length(); if (lengthDifference > 0) { String zeroPadding = new String(new char[lengthDifference]).replace("\0", "0"); newDigitString = zeroPadding + newDigitString; } String firstPart = originatingName.substring(0, firstDigitChar); String lastPart = originatingName.substring(lastDigitChar + 1); return firstPart + newDigitString + lastPart; } }  static String pluralise(int n, String noun); static String joinAll(String joiner, Object ... list); static String join(String joiner, Object ... items); static String join(String joiner, List<?> list); static String formatWithComma(Integer number); static String formatTo2dpWithComma(Number number); static String formatTo2dp(Number number); static String formatTo2dpWithDot(Number number); static String formatTo0dpWithComma(Number number); static String advanceLastNumber(String originatingName); static String[] toArrayOfLines(String text); static String toIsoDate(Date date); }
TextTools { public static String advanceLastNumber(String originatingName) { if (originatingName.length() == 0) { return "1"; } int lastDigitChar = -1; int firstDigitChar = -1; for (int i = originatingName.length() - 1; i >= 0; i--) { char c = originatingName.charAt(i); if (lastDigitChar == -1 && Character.isDigit(c)) { lastDigitChar = i; } if (Character.isDigit(c)) { firstDigitChar = i; } if (! Character.isDigit(c) && firstDigitChar > -1) { break; } } if (lastDigitChar == -1) { return originatingName + "1"; } else { String oldDigitString = originatingName.substring(firstDigitChar, lastDigitChar + 1); Integer oldValue = Integer.parseInt(oldDigitString); Integer newValue = oldValue + 1; String newDigitString = newValue.toString(); int lengthDifference = oldDigitString.length() - newDigitString.length(); if (lengthDifference > 0) { String zeroPadding = new String(new char[lengthDifference]).replace("\0", "0"); newDigitString = zeroPadding + newDigitString; } String firstPart = originatingName.substring(0, firstDigitChar); String lastPart = originatingName.substring(lastDigitChar + 1); return firstPart + newDigitString + lastPart; } }  static String pluralise(int n, String noun); static String joinAll(String joiner, Object ... list); static String join(String joiner, Object ... items); static String join(String joiner, List<?> list); static String formatWithComma(Integer number); static String formatTo2dpWithComma(Number number); static String formatTo2dp(Number number); static String formatTo2dpWithDot(Number number); static String formatTo0dpWithComma(Number number); static String advanceLastNumber(String originatingName); static String[] toArrayOfLines(String text); static String toIsoDate(Date date); final static DateFormat ISO_DATE_FORMAT; }