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

后端 未结 4 1208
感动是毒
感动是毒 2021-02-03 16:54

I originally called String.format this way:

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

Which made Androi

相关标签:
4条回答
  • 2021-02-03 17:24

    Cleaning and rebuilding the project should work.

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

    0 讨论(0)
  • 2021-02-03 17:31

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

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

    0 讨论(0)
  • 2021-02-03 17:33

    Simply add Your Locale; for English Locale,

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

    0 讨论(0)
  • 2021-02-03 17:43

    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

    0 讨论(0)
提交回复
热议问题