I am developing a simple android app.In that i want to store a string value like "1_2_5_7_12_".After that want to split this string and have to get the numbers.How to store this string.Sharedpreference or any other help?
9 Answers
String s = "1_2_5_7_12_"; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Editor edit = preferences.edit(); edit.putString("pref_str", s); edit.commit();//storing // Retrieve String pref_numstr = preferences.getString("pref_str", "n/a"); ar = pref_numstr.split("_"); System.out.println(ar.length); 1 Comment
Madhumitha
String pref_numstr = preferences.getString("pref_str", "n/a"); in this line what is "n/a"?
try as using Pattern.compile for split String to Array:
String str = "1_2_5_7_12_"; String[] strarray=Pattern.compile("-").split(str); and for storing or retrieve value from SharedPreferences see
http://developer.android.com/guide/topics/data/data-storage.html#pref
Comments
Save like this:
SharedPreferences prefs = getApplicationContext().getSharedPreferences("prefs", Context.MODE_PRIVATE); Editor prefsEditor = prefs .edit(); prefsEditor.putString("myString", "1_2_5_7_12"); prefsEditor.commit(); and retrieve like this:
String str= prefs.getString("myString", ""); after that you can split your string simply by doing
String[] strArr = str.split("_");