Does SharedPreferences use a certain character set?

廉价感情. 提交于 2019-12-11 01:35:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!