1. 程式人生 > >替換{0}為指定的字符串(MessageFormat)

替換{0}為指定的字符串(MessageFormat)

argument == scrip nts ring cti rip true turn

 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>  

  

替換{0}為指定的字符串(MessageFormat)