例如:
String a="{0} is require!";
String name="username";
String result=java.text.MessageFormat.format(a,new String[]{name});
那么,result就变成了
 
username is require!现在的问题是
a="'{0}' is require!";我是这么写的,先把'替换成",然后格式化,最后再把"换成'
a=a.replaceAll("'","\"");
String result=java.text.MessageFormat.format(a,new String[]{name});
result=result.replaceAll("\"","'");
虽然结果可以,但是,有没有更好的方法

解决方案 »

  1.   

    不是太懂你的意思哦,不知道是不是这样替换的。
    String a="'{0}' is require!";
    String name="username";
    a=a.replaceAll("'","\"");
    String result=java.text.MessageFormat.format(a,new String[]{name});
      

  2.   

    懂了点,好像是这样
    String a="'{0}' is require!";
    String name="username";
    String result=java.text.MessageFormat.format(a.substring(1),new String[]{name});
      

  3.   

    String a="''{0}'' is require!";
    我试着改成了这样就好用了。
      

  4.   

    噢噢噢噢!
    API里面如是说:
    MessageFormat uses patterns of the following form: 
     MessageFormatPattern:
             String
             MessageFormatPattern FormatElement String FormatElement:
             { ArgumentIndex }
             { ArgumentIndex , FormatType }
             { ArgumentIndex , FormatType , FormatStyle } FormatType: one of 
             number date time choice FormatStyle:
             short
             medium
             long
             full
             integer
             currency
             percent
             SubformatPattern String:
             StringPartopt
             String StringPart StringPart:
             ''
             ' QuotedString '
             UnquotedString SubformatPattern:
             SubformatPatternPartopt
             SubformatPattern SubformatPatternPart SubFormatPatternPart:
             ' QuotedPattern '
             UnquotedPattern
     Within a String, "''" represents a single quote. A QuotedString can contain arbitrary characters except single quotes; the surrounding single quotes are removed. An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''". 所以上面的写法没问题。