热门关键字: jsp web pl/sql pl webwor   →开发工具  操作系统
当前位置 : 无忧IT编程网 > Java开发 > JAVA技巧 > 正文

java格式化消息(参数替换)

来源:转载作者:无忧时间:08-03-14

代码如下:

import java.text.MessageFormat;
import java.util.Locale;
public class Test{

//这个方法,使用中国地区设置,将数组中的值替代信息字符串msgtext中的{0}和{1}等。
public static String substituteParams(Locale locale, String msgtext, Object params[]) {

   String localizedStr = null;
  if (params == null || msgtext == null) {
     return msgtext;
   }
   StringBuffer b = new StringBuffer(100);
   MessageFormat mf = new MessageFormat(msgtext);
   if (locale != null) {
           mf.setLocale(locale);
          b.append(mf.format(params));
           localizedStr = b.toString();
    }
    return localizedStr;
}

public static void main(String args[]){
 

    String msgtext="我由于{0},不能去{1}。争取在{2}去。";
    String params[]={"有事","北京","明年"};
    String s=substituteParams(Locale.CHINESE,msgtext,params);
    System.out.println(s);
  }
}

运行结果:

D:\java>javac Test.java

D:\java>java Test
我由于有事,不能去北京。争取在明年去。

D:\java>

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 验证码: 验证码
查看所有评论
相关文章
站长推荐