开发者

Is there a utility in the Android SDK or in Java to replace parts of a string using placeholders?

开发者 https://www.devze.com 2023-01-14 06:05 出处:网络
It would work something like this someUtility.replace (\"Hello, my name is {1}.What开发者_运维技巧 is your {2}?\", \"Mark\", \"name\");

It would work something like this

someUtility.replace ("Hello, my name is {1}.  What开发者_运维技巧 is your {2}?", "Mark", "name");


String.format("Hello, my name is %1$s.  What is your %2$s?", "Mark", "name");

or if you will just use each once in order:

String.format("Hello, my name is %s.  What is your %s?", "Mark", "name");


Look at MessageFormat:

MessageFormat.format("Hello, my name is {0}.  What is your {1}?", "Mark", "name");

Usually I prefer String.format or System.out.printf though for c-style formatting (sort of), as Matthew suggested.


If you want to use Spring the Resource Bundles will give you the actual exact style of replacement that you have displayed. You can see some examples here: http://www.mkyong.com/spring/spring-resource-bundle-with-resourcebundlemessagesource-example/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号