首页 > 编程语言 > 详细

java占位符

时间:2016-11-27 20:09:45      阅读:227      评论:0      收藏:0      [点我收藏+]
String str="我是{0},我来自{1},今年{2}岁,{3}";
   String[] arr={"中国人","北京","22","谢谢"};
   Matcher m=Pattern.compile("\\{(\\d)\\}").matcher(str);
        while(m.find()){
            str=str.replace(m.group(),arr[Integer.parseInt(m.group(1))]);
        }
        System.out.println(str);



Map map=new HashMap();
map.put("msgSource", "N");
map.put("title", "标题");
map.put("cnt", "内容");
String str="[title],您有一条新消息,[cnt]11";
  
        String regex = "\\[(\\w+)\\]";   
        Matcher m=Pattern.compile(regex).matcher(str);
        while(m.find()){
        if(map.get(m.group().substring(1, m.group().length()-1))!=null){
        str=str.replace(m.group(),map.get(m.group().substring(1, m.group().length()-1)));
        }
        else{
        str=str.replace(m.group(),"");
        }
        }
        System.out.println(str);

  

java占位符

原文:http://www.cnblogs.com/go4mi/p/6106896.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!