例如:
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("\"","'");
虽然结果可以,但是,有没有更好的方法
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("\"","'");
虽然结果可以,但是,有没有更好的方法
解决方案 »
- swing jpanel 重绘
- 打包成jar可执行文件 双击运行时出现Could not find the main class propram will exit!希望大虾指导下 感激!!
- 一道java网络编程的题~~
- 别人的javabean 该怎么样调用呢,我这么做为什么不好使呢?????????
- Netbeans下设计GUI问题
- 请问如何获取在鼠标在屏幕上画一下得到的那个矩形?
- 怎么固定JInternalFrame不被拖动
- 在jBuilder中如何将一个已存在的包添加到另一个project中?
- applet的使用
- 这是一个bug of something else?
- 线程和类(内部类和外部类)的一些问题?/
- 关于时间线程time用法
String a="'{0}' is require!";
String name="username";
a=a.replaceAll("'","\"");
String result=java.text.MessageFormat.format(a,new String[]{name});
String a="'{0}' is require!";
String name="username";
String result=java.text.MessageFormat.format(a.substring(1),new String[]{name});
我试着改成了这样就好用了。
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}'''". 所以上面的写法没问题。