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>  

  

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
猜你喜欢
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2022-12-23
  • 2021-09-10
  • 2021-11-19
相关资源
相似解决方案