String (String s, int numberInIteration, int totalIterations, @Nullable String surroundedText) { String by = Integer.toString(numberInIteration + 1); StringBuilder builder = new StringBuilder(s.length()); int markerStartIndex = -1; int i = 0; int n = s.length(); while (i <= n) { char c = i < n ? s.charAt(i) : 0; if (c == '\\' && i < n - 1) { i++; builder.append(s.charAt(i)); } else if (c == NUMBER_IN_ITERATION_PLACE_HOLDER && (i == n - 1 || s.charAt(i + 1) != '#')) { if (markerStartIndex == -1) { markerStartIndex = i; } } else { int markersCount = i - markerStartIndex; if (markerStartIndex != -1) { // counter base boolean decrement = false; if(i < n && s.charAt(i) == '@') { i++; if (i < n && s.charAt(i) == '-') { decrement = true; i++; } StringBuilder base = new StringBuilder(); while (i < n && Character.isDigit(s.charAt(i))) { base.append(s.charAt(i)); i++; } int baseInt = StringUtil.parseInt(base.toString(), 0) - 1; baseInt = Math.max(baseInt, 0); int byInt = decrement ? totalIterations - numberInIteration : numberInIteration + 1; byInt += baseInt; by = Integer.toString(byInt); } for (int k = 0, m = markersCount - by.length(); k < m; k++) { builder.append('0'); } builder.append(by); markerStartIndex = -1; c = i < n ? s.charAt(i) : 0; } if (i < n) { if (c == NUMBER_IN_ITERATION_PLACE_HOLDER && surroundedText != null) { builder.append(surroundedText); i++; } else { builder.append(c); } } } i++; } return builder.toString(); }