1

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?

1
  • Have you tried anything by yourself ?? Commented Dec 5, 2012 at 6:15

9 Answers 9

3
  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);
Sign up to request clarification or add additional context in comments.

1 Comment

String pref_numstr = preferences.getString("pref_str", "n/a"); in this line what is "n/a"?
1

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.

Comments

1

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

0

to save

SharedPreference.edit().putString(value, default).commit()

to get value

SharedPreference.getString(value, default)

to split the string

String.split("_") to split

Comments

0

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

Comments

0

you can do by this..

            final SharedPreferences pref1 = getSharedPreferences("My_App", MODE_PRIVATE);
            SharedPreferences.Editor editor = pref1.edit();

            editor.putString("str", "yourString");
            editor.commit();

Comments

0

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("_");

Comments

0

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("_");

Comments

0

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);
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.