Java 格式化文本的方法?

Java Method for Formatting Text?

我目前正在学习 Python,我喜欢这种无需执行

即可将变量插入字符串的方式

"+ var +" 或

" ", var, " "

示例:"We can just go out at {} pm and go to {}.".format(5, "Bob's Burgers");

有没有办法用 Java 做到这一点?

使用 "System.out.printf" 和占位符,如 %d 、 %s

public class HelloWorld {
    public static void main(String[] args) {
        String a ="Hello";
        String b = "World";
        System.out.printf("%s %s",a,b);
    }
}

是的。你可以做

String s = String.format("We can just go out at %d pm and go to %s.", 5, "Bob's Burgers");