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; }
@Test public void testAdvanceLastNumber() { String advanced = TextTools.advanceLastNumber("S1"); Assert.assertEquals("S2", advanced); }
@Test public void testAdvanceLastNumber2() { String advanced = TextTools.advanceLastNumber("S2-1.1"); Assert.assertEquals("S2-1.2", advanced); }
@Test public void testAdvanceLastNumber3() { String advanced = TextTools.advanceLastNumber("1"); Assert.assertEquals("2", advanced); }
@Test public void testAdvanceLastNumber4() { String advanced = TextTools.advanceLastNumber("foo"); Assert.assertEquals("foo1", advanced); }
@Test public void testAdvanceLastNumber5() { String advanced = TextTools.advanceLastNumber("a99f"); Assert.assertEquals("a100f", advanced); }
@Test public void testAdvanceLastNumberWithZeroPadding1() { String advanced = TextTools.advanceLastNumber("a01f"); Assert.assertEquals("a02f", advanced); }
@Test public void testAdvanceLastNumberWithZeroPadding2() { String advanced = TextTools.advanceLastNumber("a09f"); Assert.assertEquals("a10f", advanced); }