79

I'm looking for something to achieve the following:

String s = "hello {}!";
s = generate(s, new Object[]{ "world" });
assertEquals(s, "hello world!"); // should be true

I could write it myself, but It seems to me that I saw a library once which did this, probably it was the slf4j logger, but I don't want to write log messages. I just want to generate strings.

Do you know about a library which does this?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
ndrizza
  • 3,285
  • 6
  • 27
  • 41
  • 4
    http://stackoverflow.com/questions/3695230/how-to-use-java-string-format – assylias Jun 28 '13 at 12:22
  • 1
    The slf4J uses `org.slf4j.helpers.MessageFormatter` The code looks like this: `String str = "Hello this is {} string {}"; MessageFormatter.format(str, new String[]{"hello", "world", "blah"}).getMessage();` – Himanshu Chaudhary Oct 05 '18 at 14:49
  • Does this answer your question? [String replacement in java, similar to a velocity template](https://stackoverflow.com/questions/3655424/string-replacement-in-java-similar-to-a-velocity-template) – Mahozad Dec 18 '22 at 15:29

12 Answers12

96

See String.format method.

String s = "hello %s!";
s = String.format(s, "world");
assertEquals(s, "hello world!"); // should be true
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
  • 1
    how to replace two `String`s in the same `format()` ? example `String.format("%s %s!", ["hello", "world"]);` is this possible? – Carlos.V Jan 11 '19 at 18:02
  • 2
    @Carlos.V use `String.format("Hello %s welcome to %s!", "Carlos. V", "JAVA");` will print `Hello Carlos. V welcome to JAVA!` and you can use as many %s as you can. :) – AlokeT Aug 13 '19 at 06:05
44

StrSubstitutor from Apache Commons Lang may be used for string formatting with named placeholders:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.1</version>
</dependency>

https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/text/StrSubstitutor.html :

Substitutes variables within a string by values.

This class takes a piece of text and substitutes all the variables within it. The default definition of a variable is ${variableName}. The prefix and suffix can be changed via constructors and set methods.

Variable values are typically resolved from a map, but could also be resolved from system properties, or by supplying a custom variable resolver.

Example:

String template = "Hi ${name}! Your number is ${number}";

Map<String, String> data = new HashMap<String, String>();
data.put("name", "John");
data.put("number", "1");

String formattedString = StrSubstitutor.replace(template, data);
Morgan Courbet
  • 772
  • 1
  • 8
  • 18
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
  • 4
    The StrSubstitutor is deprecated, see [this SO post for a work around.](https://stackoverflow.com/questions/14044715/strsubstitutor-replacement-with-jre-libraries) – KeaganFouche Sep 20 '18 at 19:46
  • The `org.apache.commons.text.StringSubstitutor` from `org.apache.commons:commons-text:x.y` has taken it's place. Simply drop that one in and you're good to go without deprecation warnings. – andred Apr 29 '21 at 11:28
40

This can be done in a single line without the use of library. Please check java.text.MessageFormat class.

Example

String stringWithPlaceHolder = "test String with placeholders {0} {1} {2} {3}";
String formattedStrin = java.text.MessageFormat.format(stringWithPlaceHolder, "place-holder-1", "place-holder-2", "place-holder-3", "place-holder-4");

Output will be

test String with placeholders place-holder-1 place-holder-2 place-holder-3 place-holder-4
AbhishekB
  • 2,111
  • 3
  • 18
  • 23
  • 2
    This is the best solution when adding localization as the place holder order changes for different languages . – Manohar Oct 29 '20 at 12:28
14

If you can change the format of your placeholder, you could use String.format(). If not, you could also replace it as pre-processing.

String.format("hello %s!", "world");

More information in this other thread.

Community
  • 1
  • 1
LaurentG
  • 11,128
  • 9
  • 51
  • 66
9

There are two solutions:

Formatter is more recent even though it takes over printf() which is 40 years old...

Your placeholder as you currently define it is one MessageFormat can use, but why use an antique technique? ;) Use Formatter.

There is all the more reason to use Formatter that you don't need to escape single quotes! MessageFormat requires you to do so. Also, Formatter has a shortcut via String.format() to generate strings, and PrintWriters have .printf() (that includes System.out and System.err which are both PrintWriters by default)

fge
  • 119,121
  • 33
  • 254
  • 329
  • I remember reading/testing that MessageFormat was pretty slow. – assylias Jun 28 '13 at 12:24
  • 1
    @assylias meh, I don't know, my message bundle API proposes both since I have `ResourceBundle` compatibility... But I personally always use `Formatter`. – fge Jun 28 '13 at 12:25
  • 1
    I am not sure to be honest - and in most situations it won't make a difference anyway. – assylias Jun 28 '13 at 12:26
  • `MessageFormat` does have a few advantages over `Formatter` though, particularly the possibility to embed [`ChoiceFormat`](http://docs.oracle.com/javase/6/docs/api/java/text/ChoiceFormat.html) expressions. – gustafc Jun 28 '13 at 12:36
  • 1
    @gustafc not a problem, `Formatter` has [`Formattable`](http://docs.oracle.com/javase/7/docs/api/java/util/Formattable.html), which is even more powerful ;) – fge Jun 28 '13 at 12:40
  • @fge I wouldn't say that's a clearly better option in all circumstances, the canonical example being format strings like `Found {0,choice,0#nothing|1#one thing|{0} things}` – gustafc Jun 28 '13 at 13:06
7

You won't need a library; if you are using a recent version of Java, have a look at String.format:

String.format("Hello %s!", "world");
codebox
  • 19,927
  • 9
  • 63
  • 81
6

If you can tolerate a different kind of placeholder (i.e. %s in place of {}) you can use String.format method for that:

String s = "hello %s!";
s = String.format(s, "world" );
assertEquals(s, "hello world!"); // true
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

Justas answer is outdated so I'm posting up to date answer with apache text commons.

StringSubstitutor from Apache Commons Text may be used for string formatting with named placeholders: https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringSubstitutor.html

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.9</version>
</dependency>

This class takes a piece of text and substitutes all the variables within it. The default definition of a variable is ${variableName}. The prefix and suffix can be changed via constructors and set methods. Variable values are typically resolved from a map, but could also be resolved from system properties, or by supplying a custom variable resolver.

Example:

 // Build map
 Map<String, String> valuesMap = new HashMap<>();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumped over the ${target}.";

 // Build StringSubstitutor
 StringSubstitutor sub = new StringSubstitutor(valuesMap);

 // Replace
 String resolvedString = sub.replace(templateString);
Conrad
  • 536
  • 5
  • 13
4

If you want to use some string to different placeholders, you could use pointers like this:

String.format("%1$s %2$s %1$s", "startAndEndText", "middleText");
MeLean
  • 3,092
  • 6
  • 29
  • 43
1

if you are user spring you can like this:

String template = "hello #{#param}";
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("param", "world");
    String value = new SpelExpressionParser().parseExpression(template, new TemplateParserContext()).getValue(context, String.class);
    System.out.println(value);

out put:

hello world
aimilin
  • 11
  • 2
1

Java is going to have string templates (from version 21, as a preview feature).

See the string templates proposal (JEP 430) here.

It will be something along the lines of this:

String name = "World";
String info = STR."Hello \{name}!";
System.out.println(info); // Hello World!

P.S. Kotlin is 100% interoperable with Java. It supports cleaner string templates out of the box:

val name = "World"
val info = "Hello $name!"
println(info) // Hello World!

Combined with extension functions, you can achieve the same thing the Java template processors (e.g. STR) will do.

mernst
  • 7,437
  • 30
  • 45
Mahozad
  • 18,032
  • 13
  • 118
  • 133
0

The suggestion by https://stackoverflow.com/users/4290127/himanshu-chaudhary works quite well:

String str = "Hello this is {} string {}";     
str = MessageFormatter.format(str, "hello", "world").getMessage();
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.8.0-beta4</version>
</dependency>
Antoine
  • 1,393
  • 4
  • 20
  • 26
Rishi Raj
  • 3
  • 3