替换{0}为指定的字符串(MessageFormat)

时间:2024-06-08 00:04:32
 package com.text;  

 import java.text.MessageFormat;  

 /**替换{0}为指定的字符串*/
public class MessageFormatTest {
public static void main(String[] args) {
String message = "hello {0}{1}";
message = MessageFormat.format(message ,"world","!!!");
System.out.println(message);
}
}
<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>