How do I format my string in GWT?
I made a method
Formatter format = new Formatter();
int matches = 0;
Formatter formattedString = format.forma
An extension to Daniels solution: Also supports escaping using ' and throws if a number cannot be parsed (like the JVM version does):
private static final char OPEN = '{';
private static final char CLOSE = '}';
private static final char ESCAPE = '\'';
@Override
public String format(String pattern, Object... arguments) {
if (pattern == null || pattern.isEmpty())
return "";
// Approximate the result length: format string + 16 character args
StringBuilder sb = new StringBuilder(pattern.length() + (arguments.length * 16));
int cur = 0;
int len = pattern.length();
// if escaped, then its >= 0
int escapedAtIndex = -1;
while (cur < len) {
char currentChar = pattern.charAt(cur);
switch (currentChar) {
case OPEN:
if (escapedAtIndex >= 0) {
// currently escaped
sb.append(currentChar);
} else {
// find close
int close = pattern.indexOf(CLOSE, cur + 1);
switch (close) {
case -1:
// Missing close. Actually an error. But just ignore
sb.append(currentChar);
break;
default:
// Ok, we have a close
final String nStr = pattern.substring(cur + 1, close);
try {
// Append the corresponding argument value
sb.append(arguments[Integer.parseInt(nStr)]);
} catch (Exception e) {
if (e instanceof NumberFormatException) {
throw new IllegalArgumentException(nStr +
" is not a number.");
}
// Append the curlies and the original delimited value
sb.append(OPEN).append(nStr).append(CLOSE);
}
// Continue after the close
cur = close;
break;
}
}
cur++;
break;
case ESCAPE:
// Special case: two '' are just converted to '
boolean nextIsEscapeToo = (cur + 1 < len) && pattern.charAt(cur + 1) == ESCAPE;
if (nextIsEscapeToo) {
sb.append(ESCAPE);
cur = cur + 2;
} else {
if (escapedAtIndex >= 0) {
// Escape end.
escapedAtIndex = -1;
} else {
// Escape start.
escapedAtIndex = cur;
}
cur++;
}
break;
default:
// 90% case: Nothing special, just a normal character
sb.append(currentChar);
cur++;
break;
}
}
return sb.toString();
}
This implementation and the JVM-Version both pass those tests:
// Replace: 0 items
assertFormat("Nothing to replace", "Nothing to replace");
// Replace: 1 item
assertFormat("{0} apples", "15 apples", 15);
assertFormat("number of apples: {0}", "number of apples: zero", "zero");
assertFormat("you ate {0} apples", "you ate some apples", "some");
// Replace 2 items
assertFormat("{1} text {0}", "second text first", "first", "second");
assertFormat("X {1} text {0}", "X second text first", "first", "second");
assertFormat("{0} text {1} X", "first text second X", "first", "second");
Escaping-Tests:
// Escaping with no replacement
assertFormat("It's the world", "Its the world");
assertFormat("It''s the world", "It's the world");
assertFormat("Open ' and now a second ' (closes)", "Open and now a second (closes)");
assertFormat("It'''s the world", "It's the world");
assertFormat("'{0}' {1} {2}", "{0} one two", "zero", "one", "two");
// Stays escaped (if end escape is missing)
assertFormat("'{0} {1} {2}", "{0} {1} {2}", "zero", "one", "two");
assertFormat("'{0} {1}' {2}", "{0} {1} two", "zero", "one", "two");
// No matter where we escape, stays escaped
assertFormat("It's a {0} world", "Its a {0} world", "blue");
// But we can end escape everywhere
assertFormat("It's a {0} world, but not '{1}",
"Its a {0} world, but not always", "blue", "always");
// I think we want this
assertFormat("It''s a {0} world, but not {1}",
"It's a blue world, but not always", "blue", "always");
// Triple
assertFormat("' '' '", " ' ");
// From oracle docs
assertFormat("'{''}'", "{'}");
// Missing argument (just stays 0)
assertFormat("begin {0} end", "begin {0} end");
// Throws
try {
assertFormat("begin {not_a_number} end", "begin {not_a_number} end");
throw new AssertionError("Should not get here");
} catch (IllegalArgumentException iae) {
// OK
}