2

I want put value of a at {0}. With C#, I code as:

String a="123"
String b="Xin chao: {0}, ban the nao";
String c=String.Format(b,a);

But in Java I don't know. Can you help me?

4 Answers 4

5

Java's counterpart is java.text.MessageFormat that allows placeholders like the one in your question can be replaced using format method as

String a = "123";
MessageFormat.format("Xin chao: {0}, ban the nao", a);
Sign up to request clarification or add additional context in comments.

Comments

3
String b= "Xin chao: %s";
String bF = String.format(b,"123");

2 Comments

String.format() is a static method.
Yes i forgot it and i have modified my answer right now. thanks
2

See String.format(String format, Object... args).

For instance

String s = String.format("Xin chao: %1$s", "123");

Comments

0

If you don't know where the {0} will appear, you can use String.replaceAll() method:

    String a = "123";
    String b = "Xin chao: {0}, ban the nao";
    String c = b.replaceAll("\\{0\\}", a);

You might want to check StringEscapeUtils to do automatic escaping.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.