1 package com.text;
2
3 import java.text.MessageFormat;
4
5 /**替换{0}为指定的字符串*/
6 public class MessageFormatTest {
7 public static void main(String[] args) {
8 String message = "hello {0}{1}";
9 message = MessageFormat.format(message ,"world","!!!");
10 System.out.println(message);
11 }
12 }
<script type="text/javascript">
String.prototype.format=function()
{
if(arguments.length==0) return this;
for(var s=this, i=0; i<arguments.length; i++)
s=s.replace(new RegExp("\{"+i+"\}","g"), arguments[i]);
return s;
};
alert("js实现用自符串替换占位符{0} {1} {2}".format("I", "LOVE", "YOU"));
</script>