码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-04 13:09:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:pos   print   pac   reg   public   var   class   his   alert   

 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)

标签:pos   print   pac   reg   public   var   class   his   alert   

原文地址:https://www.cnblogs.com/cxxjohnson/p/8504267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!