Skip to content

Instantly share code, notes, and snippets.

@reyabreu
Created November 29, 2018 15:47
Show Gist options
  • Save reyabreu/33345c6c8a049e1111546722c0988afd to your computer and use it in GitHub Desktop.
Save reyabreu/33345c6c8a049e1111546722c0988afd to your computer and use it in GitHub Desktop.
Adds format index specifiers to format strings that don't have them
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//helper class that adds format index specifiers to plain specifiers in format strings
public class FormatStringIndexer {
private static Pattern pattern = Pattern.compile("%[-#+\\s0,(]?\\d*(\\.\\d)?[bBhHsScCdDoxXeEfgGaAtT%n]");
public static String index(final String formatString, int start) {
final Matcher matcher = pattern.matcher(formatString);
final StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String replacement = "%" + start++ + "\\$" + matcher.group().substring(1);
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment