问题
I have just noticed that SharedPreferences removes carriage return (\r) when storing a string. Since it was quite costly for me to not know this, I would like to know if it removes any other characters. In other words, does it use a certain character set.
SharedPreferences sp = activity.getSharedPreferences("preferences", Context.MODE_PRIVATE);
sp.edit().putString("Foo", "\r\nfoo").commit();
//After restarting the app
SharedPreferences sp = activity.getSharedPreferences("preferences", Context.MODE_PRIVATE);
String sFoo= sp.getString("Foo", ""); //returns "\nfoo", not "\r\nfoo"
回答1:
Good morning,
It is even worst: if you store the String "\r" into SharedPref, after killing and restarting application, the value from SharedPref will become "\n".
Another bug from our beloved Android platform, very annoying...
Cheers
回答2:
In other words, does it use a certain character set.
When you write your SharedPreferences to the disk, it uses XmlUtils.writeMapXml, which outputs in UTF-8
.
来源:https://stackoverflow.com/questions/22999610/does-sharedpreferences-use-a-certain-character-set