我正在寻找一个很好的JavaScript等价的C/PHP printf()或C#/ Java程序员String.Format()(IFormatProvider对于.NET).
我的基本要求是现在有一千个数字分隔符格式,但处理许多组合(包括日期)的东西会很好.
我意识到Microsoft的Ajax库提供了一个版本String.Format(),但我们不希望该框架的整个开销.
我发现需要在项目中使用String.format。但是,这在GWT中不起作用,我有使用GWT RegExp的替代产品。但是,我希望我的项目在使用不依赖于GWT的普通Swing接口时运行时不会出现错误。共享代码取决于String.format。问题是我不知道如何使GWT编译器使用与其他代码不同的类。我已经尝试在一个文件中完成所有操作,但是它不起作用,因为GWT无法处理NoClassDefFoundError异常,该异常用于检测classpath中缺少GWT的时间。
这是我当前的代码:
package testpackage.shared;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.regexp.shared.SplitResult;
public class Util {
public static String format(final String format, final Object... args) {
try {
final RegExp regex = RegExp.compile("%[a-z]");
final SplitResult split = regex.split(format);
final StringBuffer msg = new StringBuffer();
for (int pos = 0; pos < split.length() - 1; pos += 1) {
msg.append(split.get(pos));
msg.append(args[pos].toString());
}
msg.append(split.get(split.length() - 1));
return msg.toString();
} catch (NoClassDefFoundError ex) {
return String.format(format, args);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何在类路径中不使用GWT的情况下在GWT和JRE上进行编译和运行?在为GWT和Swing / Sun JRE进行构建时,是否真的需要使Ant构建脚本将整个文件替换为另一个文件?