https://blog.csdn.net/sinat_30160727/article/details/78075687
- | 转义字符 | 被转义中形式 |
---|---|---|
左括号 | { | ‘{’ |
单引号 | ’ | ‘’ |
双引号 | “” | ‘"’ ‘"’ |
1.双引号
String test1 = "{0} is my frided,and {1} is my fridend.";
String test2 = "\"{0}\" is my frided,and \"{1}\" is my fridend.";
String test3 = "'\"{0}'\" is my frided,and '\"{1}'\" is my fridend.";
Object[] argument_test1 = new Object[]{"Amy","Aliese"};
String result_test1 = MessageFormat.format(test1, argument_test1);
String result_test2 = MessageFormat.format(test2, argument_test1);
String result_test3 = MessageFormat.format(test3, argument_test1);
System.out.println(test1+ "------->" +result_test1);
System.out.println(test2+ "------->" +result_test2);
System.out.println(test3+ "------->" +result_test3);
结果演示
2.括号【左括号是重点】
public static void test2() {
String test1 = "'{'0'}' is my frided,and '{'1'}' is my fridend.";
String test2 = "'{'{0}'}' is my frided,and '{'{1}'}' is my fridend.";
String test3 = "{0}} is my frided,and {1}} is my fridend.";
// String test4 = "{{0}} is my frided,and {{1}} is my fridend.";
Object[] argument_test1 = new Object[]{"Amy","Aliese"};
String result_test1 = MessageFormat.format(test1, argument_test1);
String result_test2 = MessageFormat.format(test2, argument_test1);
String result_test3 = MessageFormat.format(test3, argument_test1);
// String result_test4 = MessageFormat.format(test4, argument_test1);
System.out.println(test1+ "------->" +result_test1);
System.out.println(test2+ "------->" +result_test2);
System.out.println(test3+ "------->" +result_test3);
// System.out.println(test4+ "------->" +result_test4);
}
结果演示
但是String test4 = “{{0}} is my frided,and {{1}} is my fridend.”;里面因为包含
{ 。所以会报错
3.单引号(俩个代表一个)
public static void test3() {
String test1 = "'{0}' is my frided,and '{1}' is my fridend.";
String test2 = "''{0}'' is my frided,and ''{1}'' is my fridend.";
String test3 = "\'{0}\' is my frided,and \'{1}\' is my fridend.";
Object[] argument_test1 = new Object[]{"Amy","Aliese"};
String result_test1 = MessageFormat.format(test1, argument_test1);
String result_test2 = MessageFormat.format(test2, argument_test1);
String result_test3 = MessageFormat.format(test3, argument_test1);
System.out.println(test1+ "------->" +result_test1);
System.out.println(test2+ "------->" +result_test2);
System.out.println(test3+ "------->" +result_test3);
}
结果演示