Java Method for Formatting Text?

57 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 answers

0
Bidhan 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
Sarit Adhikari 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);
    }
}