Reputation: 3824
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?
Upvotes: 1
Views: 1840
Reputation: 2119
use this code ....
public static ArrayList<String> strDRIVERS = new ArrayList<String>();
String DNAME="1_2_5_7_12";
if(DNAMES.length()>0){
String[] arr_drivers = DNAMES.split(",");
for(String sx : arr_drivers){
strDRIVERS.add(sx);
}
}
Upvotes: 0
Reputation: 132982
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
Upvotes: 1
Reputation: 21191
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);
Upvotes: 3
Reputation: 1489
For saving string you can use SharedPreferences or string.xml file. For splitting the string
String text = "1_2_5_7_12_";
String[] splits = text.split("_");
Upvotes: 0
Reputation: 1387
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("_");
Upvotes: 0
Reputation: 4255
you can do by this..
final SharedPreferences pref1 = getSharedPreferences("My_App", MODE_PRIVATE);
SharedPreferences.Editor editor = pref1.edit();
editor.putString("str", "yourString");
editor.commit();
Upvotes: 0
Reputation: 4860
Eventually it's gonna be a string, so i do not see any problem to store it in Sharedpreference . And you can split it as in @Shreya Shah's response
Upvotes: 0
Reputation: 54682
to save
SharedPreference.edit().putString(value, default).commit()
to get value
SharedPreference.getString(value, default)
to split the string
String.split("_") to split
Upvotes: 0
Reputation: 15644
You can split the string :
String myString = "1_2_5_7_12_";
String numbers[] = myString.split("_");
int num[] = new int[numbers.length];
int i=0;
for(String s : numbers){
num[i] = Integer.parseInt(s);
i++;
}
So now the array num
will contain integers in that string.
Upvotes: 1