tags:

views:

1389

answers:

9

Is there a more elegant way of doing this in Java?

String value1 = "Testing";  
String test = "text goes here " + value1 + " more text";

Is it possible to put the variable directly in the string and have its value evaluated?

A: 

What you want is called String interpolation. It is not possible in Java, although JRuby, Groovy and probably other JVM languages do that.


Edit: as for elegance, you can use a StringBuffer or check the other poster's solution. But at the low level, this will always be concatenation, as the other posters said.

Miguel Ping
+1  A: 

It may be done by some template-libaries. But beware, Strings are immutable in Java. So in every case at some low level the concatenation will be done.

Mnementh
+14  A: 
   String test = String.format("test goes here %s more text", "Testing");

is the closest thing that you could write in Java

dfa
Thanks for the answer. I think I'll stick with concatenation, more readable with lots of variables.
me_here
+1  A: 

You'll always have to use some form of concatenation for this (assuming value1 isn't a constant like you show here).

The way you've written it will implicitly construct a StringBuilder and use it to concatenate the strings. Another method is String.format(String, Object...)1, which is analogous to sprintf from C. But even with format(), you can't avoid concatenation.

1 Yes, I know the anchor link is broken.

Michael Myers
A: 

You can use this free library. It gives you sprintf like functionality. Or use String.format static method provided you use Java 5 or newer.

Pablo Santa Cruz
+1  A: 

A more elegant way might be:

 String value = "Testing"; 
 String template = "text goes here %s more text";
 String result = String.format(template, value);

Or alternatively using MessageFormat:

 String template = "text goes here {0} more text";
 String result = MessageFormat.format(template, value);
toolkit
A: 

Why do you think string concatenation isn't elegant?

If all you are doing is simple concatenation, I'd argue that code readability is more important and I'd leave it like you have it. It's more readable than using a StringBuilder.

Performance won't be the problem that most people think it is.

Read this from CodingHorror

A_M
I agree, I'll stick with concatenation over the .format method. I was hoping for string evaluation similar to PHP.
me_here
A: 

I would use a StringBuffer.. it's a common practise when you are dealing with strings. It may seem a bit when you see it for the first time, but you'll get quickly used to it..

String test = new StringBuffer("text goes here ").append(value1).append(" more text").toString();

Strings are immutable thus a new instance is created after every concatenation. This can cause performance issues when used in loops.

StringBuffer is mutable version of String - that means you can create one, modify it as you want and you have still only one instance. When desired you can get a String representation of the StringBuffer by calling it's toString() method.

Martin Lazar
A: 

The problem is not if this is an elegant way or not. The idea behind using a template system may be that you put your template in a normal text file and don't have to change java code if you change your message (or think about i18ln).

Tom