Inserting a Java string into another string without concatenation?

Is there a more elegant way to do this in Java?

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

Is it possible to put a variable directly in a string and evaluate its value?

+21
java string
Apr 21 '09 at 14:56
source share
10 answers
  String test = String.format("test goes here %s more text", "Testing"); 

is the closest thing you could write in Java

+49
Apr 21 '09 at 2:59 p.m.
source share

A more elegant way could 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); 



Note that if you do this for logging, you can avoid the expense of doing this when the log line is below the threshold. For example, SLFJ :

The next two lines will lead to the same result. However, the second form will be superior to the first form by at least 30 times in the case of a disabled journal entry.

 logger.debug("The new entry is "+entry+"."); logger.debug("The new entry is {}.", entry); 
+25
Apr 21 '09 at 15:05
source share

Rythm java template engine is now released with a new function String Interpolation Mode , which allows you to do something like

 String result = Rythm.render("Hello @who!", "world"); 

In the above example, you can pass an argument to a pattern by position. Rythm also lets you pass arguments by name:

 Map<String, Object> args = new HashMap<String, Object>(); args.put("title", "Mr."); args.put("name", "John"); String result = Rythm.render("Hello @title @name", args); 

References:

+3
Jul 01 '12 at 7:44
source share

This can be done with some templates. But be careful, Strings are immutable in Java. Therefore, in each case, at some low level, concatenation will occur.

+1
Apr 21 '09 at 2:59 p.m.
source share

You always need to use some form of concatenation (if value1 is not a constant, as shown here).

The way you wrote it will implicitly build a StringBuilder and use it to concatenate strings. Another method is String.format(String, Object...) 1 which is similar to sprintf from C. But even with format() , you cannot avoid concatenation.

1 Yes, I know that the link to the anchor is broken.

+1
Apr 21 '09 at 2:59 p.m.
source share

What you want is called String Interpolation . This is not possible in Java, although JRuby , Groovy, and possibly other JVM languages ​​do this.




Edit: for elegance, you can use StringBuffer or check out another poster solution. But at a low level, it will always be concatenation, as other posters have said.

0
Apr 21 '09 at 14:57
source share

You can use this free library . This gives you sprintf functionality. Or use the String.format static method if you are using Java 5 or later.

0
Apr 21 '09 at 15:01
source share

Why do you think string concatenation is not elegant?

If all you do is simply concatenate, I would argue that code readability is more important, and I would leave it as you have. This is more readable than using StringBuilder.

Performance will not be a problem that many people think of.

Read this from CodingHorror

0
Apr 21 '09 at 15:05
source share

I would use a StringBuffer. This is a common practice when dealing with strings. It may seem a little when you see it for the first time, but you will quickly get used to it.

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

Strings are immutable, so after each concatenation a new instance is created. This can cause performance problems when used in cycles.

StringBuffer is a mutable version of String . This means that you can create it, change it as you want, and you have only one instance. You can optionally get a string representation of a StringBuffer by calling it toString () .

0
Apr 21 '09 at 15:41
source share

The problem is not that this is an elegant way or not. The idea of ​​using a template system may be that you put your template in a plain text file and should not change the Java code if you change your message (or think about i18ln).

0
Aug 04 2018-10-12T00:
source share



All Articles