How to use toUppercase (locale) in android 4.2?

enter image description here I am using the android 4.2 SDK SDK, I get a warning in this line of code:

String text0 = tagalogText.getText().toString(); String textA = text0.substring(0, 1).toUpperCase() + text0.substring(1).toLowerCase(); 

When I hung over him, he says:

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

and

Implicitly using a standard locale is a common source of errors: use toLowerCase (Locale) instead.

I am copying code from Java, not Java for android. Does anyone know how to remove this error? And why now is this the preferred way to use this method?

+7
source share
6 answers

Just need to clean up the project .

I had the same problems. I did not rely on the standard locale and obviously tried to use Locale.US and Locale.English, but still got a yellow warning. Gone after cleaning.

 string.toLowerCase(Locale.ENGLISH); 
+5
source

This is a warning, not an error. The reason is that the default locale should be used for data accessed by the user - if you show what the user might prefer to see in a style / format specific to their locality.

You need to decide whether to use the standard locale:

  • if this is a string that will be used behind the scenes, and your architecture expects this string to be the same for all devices around the world, then you must specify the locale explicitly - usually Locale.US

  • if this is a line facing the user, or your architecture does not care about whether it differs on other devices (i.e. it is used only within the country and is not expected in a specific format other than the specified case), then you can safely ignore warning lint.

+2
source

Try using

 text0.substring(0, 1).toUpperCase(Locale.ENGLISH); 

From the document here

A common mistake is the implicit use of the default standard when producing products intended for machine read. This tends to work as developer test devices (especially because so many developers use en_US), but it does not execute when run on a device whose user is in a more complex language.

For example, if you format integers, some locales will use non-ASCII decimal digits. As another example, if you format floating point numbers, some locales will use ',' as the decimal point as well as '.' to group numbers. This is correct for human reading, but can cause problems if they are presented on another computer (parseDouble (String) cannot parse such a number, for example). You should also beware of overloads of toLowerCase () and toUpperCase () which do not take Locale: in Turkey, for example, the characters 'i' and 'I' will not be converted to 'I' and 'I'. This is correct behavior for Turkish text (e.g. user input), but inappropriate, say, HTTP headers.

0
source

This means that there may be language issues using the standard Locale, which is system dependent. Use this instead:

 text0.substring(0, 1).toUpperCase(Locale.US); 
0
source

You didn't indicate that you have an answer, so try (something like this worked for me):

 text0 = text0.substring(0, 1).toUpperCase(); text1 = text0.substring(1).toLowerCase(); String textA = text0 + text1; 

Appointment seems to be crucial.

Let me know if it works.

Take care.

0
source

Here's how I solved it:

 string.toUpperCase(getResources().getConfiguration().locale)); 
0
source

All Articles