Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

java - Correct format string for String.format or similar

I'm sure I've seen String.format used like this before:

String.format("Some {1}, {2}, {3}", var1, var2, var3);

Does this ring any bells for anyone? Maybe I'm thinking of C#, is there a way of achieving the same in java?

I know you can do something like:

String.format("Some %s, %d, %s", aString, aNumber, aString)

but the syntax I'm after is for the former...

question from:https://stackoverflow.com/questions/12132604/correct-format-string-for-string-format-or-similar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you are looking for is MessageFormat, which uses a given format and input parameters, e.g.

MessageFormat.format("Some {0}, {1}, {2}", var1, var2, var3);

And as already mentioned, String.format can still do the job using the alternate syntax, but it is less powerful in functionality and not what you requested.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...