2

What is the equivalent Java implementation for string.format?

string s = string.Format("{0} - ({1})", myparam1, myparam2);

4 Answers 4

8
String formatted = String.format("%s - (%s)", myparam1, myparam2);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was interested in the Message format string %s - (%s)
3

Err... String.format seems like a pretty good candidate. Who would have thought? :-)

There is also MessageFormat.format.

Comments

1

Accepted answer is correct. I would just like to add the Java equivalent of argument index like OP has used in his post. Please, note that I have reversed the argument positions but they would still print in order.

String fmtStr = String.format("%2$s - (%1$s)", myparam2, myparam1);

Format-string argument indices start from 1 in Java.

Comments

1

in Java, String.format patterns are different like %s or %1$d etc.
if you are looking for dotnet similarity it is Messageformat.format

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.