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

后端 未结 4 1197
感动是毒
感动是毒 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: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

提交回复
热议问题