简体   繁体   中英

java.util.UnknownFormatConversionException: Conversion = '#'

This my code and it throws an exception while formatting the string with use of String.Format Java native method.

String value = "#%1$s#.*?%#";
String patt = String.format(value);    

java.util.UnknownFormatConversionException: Conversion = '#'

at java.util.Formatter.checkText(Unknown Source)

at java.util.Formatter.parse(Unknown Source)

at java.util.Formatter.format(Unknown Source)

at java.util.Formatter.format(Unknown Source)

at java.lang.String.format(Unknown Source)

Any idea what's wrong?

you have to add string format to format the string you want. Ex :

String value = "#%1$s#.*?%#";
String patt = String.format("%s",value);

System.out.println(patt);

Read this

There are two format methods:

public static String format(String format, Object... args)  

and

public static String format(Locale locale, String format, Object... args)  

locale : specifies the locale to be applied on the format() method.

format : format of the string.

args : arguments for the format string. It may be zero or more

So in your case, whatever format you are specifying is not proper I guess.

You can check different format which you can use over here: https://dzone.com/articles/java-string-format-examples

The value:

String value = "#%1$s#.*?%#";

can't contain the character % . It is used as a reserverd word.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM