虽然 GWT 不能模拟所有 java 的核心,但可以用作以下替代方案:
String.format("The answer is - %d", 42)?
在 GWT 中向消息注入(inject)参数的优雅高效模式是什么?
最佳答案
你可以自己写。
我写了一个只适用于 Strings(%s) 的版本:
public static String format(final String format, final Object... args)
{
checkNotNull(format);
checkNotNull(args);
final String pattern = "%s";
int start = 0, last = 0, argsIndex = 0;
final StringBuilder result = new StringBuilder();
while ((start = format.indexOf(pattern, last)) != -1)
{
if (args.length <= argsIndex)
{
throw new IllegalArgumentException("There is more replace patterns than arguments!");
}
result.append(format.substring(last, start));
result.append(args[argsIndex++]);
last = start + pattern.length();
}
if (args.length > argsIndex)
{
throw new IllegalArgumentException("There is more arguments than replace patterns!");
}
result.append(format.substring(last));
return result.toString();
}
关于gwt - GWT 中 String.format() 的替代方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15452994/