Why does Android Lint warn about String.format using default locale when explicitly using Locale.US?

不打扰是莪最后的温柔 提交于 2021-02-05 20:40:12

问题


I originally called String.format this way:

return String.format("%s %f %f", anotherString, doubleA, doubleB);

Which made Android Lint generate this warning:

Implicitly using the default locale is a common source of bugs: Use String.format(Locale, ...) instead

So I changed it to use Locale.US explicitly, based on what I read at http://developer.android.com/reference/java/util/Locale.html under the "Be wary of the default locale" section:

return String.format(Locale.US, "%s %f %f", anotherString, doubleA, doubleB);

Why does Android Lint still generate the same warning? I have to clean the project in Eclipse to get rid of it, when most warnings just disappear as soon as the offending line is fixed. I'm not sure if I'm doing something wrong or not.


回答1:


Cleaning and rebuilding the project should work.

BTW, you may want to use Locale.getDefault() to "take care" of texts not written in english.




回答2:


Implied default locale in case conversion

Calling String#toLowerCase() or #toUpperCase() without specifying an explicit locale is a common source of bugs. The reason for that is that those methods will use the current locale on the user's device, and even though the code appears to work correctly when you are developing the app, it will fail in some locales. For example, in the Turkish locale, the uppercase replacement for i is not I.

If you want the methods to just perform ASCII replacement, for example to convert an enum name, call String#toUpperCase(Locale.US) instead. If you really want to use the current locale, call String#toUpperCase(Locale.getDefault()) instead.

http://developer.android.com/reference/java/util/Locale.html#default_locale




回答3:


when I mentioned the locale with the format, the lint warning just vanished.

String.format(Locale.US,"%02d", selectedInt);




回答4:


Simply add Your Locale; for English Locale,

return String.format(Locale.ENGLISH,"%s %f %f", anotherString, doubleA, doubleB);



来源:https://stackoverflow.com/questions/16927664/why-does-android-lint-warn-about-string-format-using-default-locale-when-explici

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!