Java Method for Formatting Text?

77 Views Asked by At

I'm currently taking Python, and I like the way one is able to insert variables into a string without doing

"+ var +" or

" ", var, " "

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

Is there a way to do it with Java?

2

There are 2 best solutions below

0
On BEST ANSWER

Yes. You could do

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

Use "System.out.printf" and placeholders like %d , %s

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