Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arabic characters in String.Format("%d",1,Locale.US)

Tags:

java

android

I have the following code:

private static final String PATTERN = "file_%d.txt";
int no; // 1-3
String filename = String.format(PATTERN, no ,Locale.US);

and later on I get an exception saying that

java.io.FileNotFoundException: file_٣.txt

which indicates that %d got replaced with an arabic number. How can that be if I explicitely specify Locale.US?

like image 303
tmanthey Avatar asked Oct 03 '13 11:10

tmanthey


People also ask

What is %d in string in Java?

The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: The value of i is: 461012. The printf and format methods are overloaded.

What is %s in string format?

%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.

What does %n do when used in a format string Java?

By using %n in your format string, you tell Java to use the value returned by System. getProperty("line. separator") , which is the line separator for the current system.


1 Answers

The locale needs to be the first parameter:

 String.format(Locale.US,PATTERN, no);
like image 78
Thilo Avatar answered Oct 06 '22 14:10

Thilo