12

There are already several questions about the MessagingFormat in general, but I haven't found anything yet that answers my question. I'm aware, that single quotes will break the pattern. If you use MessageFormat or Log4j something like "doesn't" can break possible placeholders.

See Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. A single quote itself must be represented by doubled single quotes '' throughout a String.

Simple example:

@Test
public void test() {
    String pattern = "{0} doesn't show values ( {1}, {2}, {3}, {4} )";
    final Object[] args = { "Testpattern", 100, 200, 300, 400 };
    System.out.println(MessageFormat.format(pattern, args));
    pattern = pattern.replaceAll("(?<!')'(?!')", "''");
    System.out.println("Replaced singlequotes: " + MessageFormat.format(pattern, args));
}

Output:

Testpattern doesnt show values ( {1}, {2}, {3}, {4} )
Replaced singlequotes: Testpattern doesn't show values ( 100, 200, 300, 400 )

So, if I replace all single quotes using a regular expression, it will work. I just made up the regular expression trying to only replace "single singlequotes" using regular expression lookahead/lookbehind.

Regular expression replace examples:

    doesn't -> doesn''t
    doesn''t -> doesn''t
    doesn'''t -> doesn'''t

I just wonder, if any apache-commons utility (or any other library) exists, which will handle the "escapeSingleQuotes" for me instead of providing my own regular expression...?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
proko
  • 1,180
  • 1
  • 7
  • 14

5 Answers5

19

It helped me to do something like this:

`doesn''t`
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mat
  • 191
  • 1
  • 3
  • 1
    Yes this works if you are aware of single quotes and MessageFormat. But providing a framework relying on developers always writing two quotes is no good choice. – proko Jul 08 '14 at 05:00
  • The recommendation from ICU is to use the ASCII apostrophe (' U+0027) only for escaping syntax characters, and use the pretty single quote (’ U+2019) for actual apostrophes and single quotes in a message pattern. – ChatGPT Aug 22 '15 at 04:03
7

The recommendation from ICU is to use the ASCII apostrophe (' U+0027) only for escaping syntax characters, and use the pretty single quote (’ U+2019) for actual apostrophes and single quotes in a message pattern.

ChatGPT
  • 5,334
  • 12
  • 50
  • 69
  • Yes, that would indeed help. Our framework provides a logger and it should be aware of single quotes and not breaking message format. Since others might use the logger, we cannot control if every developer is using proper quotes. – proko Aug 24 '15 at 06:57
5

For everyone that has Android problems in the string.xml, use \'\' instead of single quote.

Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
3

Since I haven't found another API which would provide the escaped Singlequote (' -> '') I will stay with my regular expression.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
proko
  • 1,180
  • 1
  • 7
  • 14
0

Use the escapeEcmaScript method from Apache Commons Lang package.

See Stack Overflow questions:

Community
  • 1
  • 1
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
  • 1
    No, StringEscapeUtils.escapeEcmaScript() will break the MessageFormat / logging. (Output: Replaced singlequotes: Testpattern doesn\t show values ( {1}, {2}, {3}, {4} )) – proko Jul 09 '13 at 09:44