2

Or: is there a another utility capable of both formatting strings and parsing strings (using the same object!) that can achieve this?

Suppose you have a directory full of audio files in .au format with names following the pattern country.code.XX.au. We might like to format and parse the significant parts of these filenames using a MessageFormat with pattern country.{0}.au.

But consider this demo:

public class MessageFormatExercise {
    public static void main(String[] args) throws Exception {
        java.text.Format f = new java.text.MessageFormat("country.{0}.au");

        // String formatting; prints "country.code.us.au" and "country.code.au.au" respectively
        System.out.println(f.format(new Object[]{"code.us"}));
        System.out.println(f.format(new Object[]{"code.au"}));

        // String parsing; prints "code.us" and "code" respectively (not "code.au"!)
        System.out.println(((Object[]) f.parseObject("country.code.us.au"))[0]);
        System.out.println(((Object[]) f.parseObject("country.code.au.au"))[0]);
    }
}

The parse results are not consistent. In some cases we get code.us or code.gb but in the case of Australia we get simply code.

Parforma
  • 21
  • 2
  • The modernized `com.ibm.icu.text.MessageFormat` with named template parameters and saner quoting rules exhibits the same behavior, yielding `code` rather than `code.au` – Parforma Aug 13 '16 at 19:01

0 Answers0