How do I format my string in GWT?
I made a method
Formatter format = new Formatter();
int matches = 0;
Formatter formattedString = format.forma
As mentionned above, there is GWT formatters for numbers and dates : NumberFormat
and DateTimeFormat
.
Still, I needed a solution for the well-know String.format(...)
case.
I end up with this solution, I don't know if it's wrong for performance, but it's visually clean. I'd be glad to hear any comment on it, or about other solution.
My String formatter :
public class Strings {
public static String format(final String format, final Object... args) {
String retVal = format;
for (final Object current : args) {
retVal = retVal.replaceFirst("[%][s]", current.toString());
}
return retVal;
}
}
and the JUTest if one want to re-use this :
public class StringsTest {
@Test
public final void testFormat() {
this.assertFormat("Some test here %s.", 54);
this.assertFormat("Some test here %s and there %s, and test [%s]. sfsfs !!!", 54, 59, "HAHA");
}
private void assertFormat(final String format, final Object... args) {
Assert.assertEquals("Formatting is not working", String.format(format, args), Strings.format(format, args));
}
}