I am trying to use MessageFormat.format() on a string containing the symbols: {
I am fetching this string from my .properties file, as a key value pair like this:
helloWorldString=hello{"hello":"{0}"}
I would like to use the
MessageFormat.format(helloWorldString, "world");
However, this throws an error java.lang.IllegalArgumentException: can't parse argument number: "hello":"%s"
I have also tried:
helloWorldString=hello%7B%22hello%22:%22%s%22%7D
to get the error: java.util.UnknownFormatConversionException: Conversion = '2'
And:
helloWorldString=hello%7B"hello":"%s"%7D
which returns the error: java.util.UnknownFormatConversionException: Conversion = 'D'
Any help on formatting these symbols, with the %s (or an alternative String placeholder) would be greatly appreciated!
Your property file entry should look like this:
helloWorldString='{'"hello":"{0}"'}'
You must escape the {
and }
symbols which do not enclose arguments with a '
.
From the JavaDocs :
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. For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}".
You're making it up. Have a look at the Javadoc . There is no %s
feature in MessageFormat
. If you want the result to be "hello world"
, the property entry should be
helloWorldString="hello {0}"
Similarly you are making this up as well:
hello%7B%22hello%22:%22%s%22%7D
There is no URL-encoding feature in a .properties file, or in MessageFormat
.
Use the documentation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.