George Newton George Newton - 13 days ago 5
Java Question

Escaping variables in java

Can you escape variables in Java? For example, in PHP, I can do this:

$test = 3;
echo "He is {$test} years old";


Which will output:

He is 3 years old


Can you do a similar thing in Java?

Answer

You should use String.format

int age = 3;
String newString = String.format("He is %d years old", age);

%d - says int. %s - is for string, %f for float, and more and more formatting options.

You can find a very good explenation here:

Noam.

Comments