有没有办法使用 SLF4J 样式的格式化函数来构建 Java 字符串? [英] Is there a way to build a Java String using an SLF4J-style formatting function?

查看:46
本文介绍了有没有办法使用 SLF4J 样式的格式化函数来构建 Java 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说使用 StringBuilder 比使用字符串连接更快,但我厌倦了一直与 StringBuilder 对象搏斗.我最近接触了 SLF4J 日志库,与 String.format 相比,我喜欢它的格式只做正确的事"的简单性.有没有一个图书馆可以让我写一些类似的东西:

I've heard that using StringBuilder is faster than using string concatenation, but I'm tired of wrestling with StringBuilder objects all of the time. I was recently exposed to the SLF4J logging library and I love the "just do the right thing" simplicity of its formatting when compared with String.format. Is there a library out there that would allow me to write something like:

int myInteger = 42;
MyObject myObject = new MyObject();  // Overrides toString()
String result = CoolFormatingLibrary.format("Simple way to format {} and {}",
    myInteger, myObject);

另外,是否有任何原因(包括性能但不包括对日期和有效数字格式的细粒度控制)为什么我可能想要在这样的库上使用 String.format(如果它确实存在)?

Also, is there any reason (including performance but excluding fine-grained control of date and significant digit formatting) why I might want to use String.format over such a library if it does exist?

推荐答案

对于连接字符串一次,旧的可靠"str"+ param + "other str" 完全没问题(它实际上被编译器转换成 StringBuilder).

For concatenating strings one time, the old reliable "str" + param + "other str" is perfectly fine (it's actually converted by the compiler into a StringBuilder).

StringBuilders 主要用于向字符串添加内容,但您无法将它们全部放入一个语句中.以for循环为例:

StringBuilders are mainly useful if you have to keep adding things to the string, but you can't get them all into one statement. For example, take a for loop:

String str = "";
for (int i = 0; i < 1000000; i++) {
    str += i + " "; // ignoring the last-iteration problem
}

这将比等效的 StringBuilder 版本运行得慢得多:

This will run much slower than the equivalent StringBuilder version:

StringBuilder sb = new StringBuilder(); // for extra speed, define the size
for (int i = 0; i < 1000000; i++) {
    sb.append(i).append(" ");
}
String str = sb.toString();

但这两者在功能上是等价的:

But these two are functionally equivalent:

String str = var1 + " " + var2;
String str2 = new StringBuilder().append(var1).append(" ").append(var2).toString();


说了这么多,我的实际答案是:

查看java.text.MessageFormat.来自 Javadocs 的示例代码:


Having said all that, my actual answer is:

Check out java.text.MessageFormat. Sample code from the Javadocs:

int fileCount = 1273;
String diskName = "MyDisk";
Object[] testArgs = {new Long(fileCount), diskName};

MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0} file(s).");

System.out.println(form.format(testArgs));

输出:

磁盘MyDisk"包含 1,273 个文件.

还有一个静态的 format 方法,不需要创建 MessageFormat 对象.

There is also a static format method which does not require creating a MessageFormat object.

所有这些库都将归结为最基本级别的字符串连接,因此它们之间不会有太大的性能差异.

All such libraries will boil down to string concatenation at their most basic level, so there won't be much performance difference from one to another.

这篇关于有没有办法使用 SLF4J 样式的格式化函数来构建 Java 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆