2013年12月14日土曜日

Androidのアプリ開発におけるStringの注意点

なんとなく普段Javaを書く感覚で開発してたところ、Eclipseに出ていたWarningが気になった。

以下、出力された渓谷
Provides more information about this issue.
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.


詳細メッセージを表示してみた。

URL書いてあったので移動(ここ)。

たぶん、文字列を扱う際に英語以外の言語に注意しろと言っていると思う。
トルコ文字の"i"と”I"は違うんだ!って書いてあった。
他の事例はわからんけど、toUpperCaseとか文字列変換を気軽にやると痛い目に会うよということらしい。
USはたくさん使ってる人いるし、使いやすい。みたいなことが書いてあってすごく適当な仕様だとおもった。

普段使う文には大丈夫なんだろうけど、Locale.USをいつも指定すれば、平気かなぁなんて考えてる。

Warningが減ったので気分はすこしすっきり。

0 件のコメント:

コメントを投稿