Saving a string of data separated by commas - android

Before I begin to work on this project, I have a couple of editTexts and when user enters the text I want to save it(as a string and add the values from additional edittexts in that string separated by commas). What is the best way to save data like this.
Another thing the previous data will not be needed when the user launches the app the next time.

There's probably a hundred different ways to make a delimited list, but to literally answer your question I'll give you three easy ways
String:
String someString = someString + "," + SOMESTRINGFROMEDITTEXT;
StringBuilder:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(SOMESTRINGFROMEDITTEXT + ",");
String someString = stringBuilder.toString();
ArrayList:
StringBuilder stringBuilder = new StringBuilder();
ArrayList<String> arrayListOfStrings = new ArrayList<String>();
arrayListOfStrings.add(SOMESTRINGFROMEDITTEXT);
for (String s : arrayListOfStrings) {
stringBuilder.append(s + ",");
}
String someString = stringBuilder.toString();

Related

Removal of comma in the end of a string android

i have problem how to , remove the comma in the of my output. I use replaceall but it doesnt remove the comma , this my code
public void onClick(View v) {
String space = "";
String foo = " ";
String foo1 = ",";
String sentences = null;
//Splitting the sentence into words
sentences = multiple.getText().toString().toLowerCase();
String[] splitwords = sentences.trim().split("\\s+");
for (String biyak : splitwords) {
foo = (foo + "'" + biyak + "'" + foo1);
foo.replaceAll(",$", " ");//foo.replaceAll();
wordtv.setText(foo);
My codes Output : 'Hello','world',
My desire output: 'Hello','world'
String instances are immutable. As a result, methods like replaceAll() do not modify the original string but instead return a modified copy of the string. So replace foo.replaceAll(...) with foo = foo.replaceAll(...).
you can use substring method of String class. or You can use deleteCharAt() method of StringBuilder or StringBuffer classStringBuffer sb=new StringBuffer(your_string);sb.deleteCharAt(sb.length()-1);
U can also use a if statement and traverse the whole string .
If a comma(,) is found replace it with a space(" ").

How to Split a string by Comma And Received them Inside Different EditText in Android?

I have Two EditText(id=et_tnum,et_pass). I Received a String like 12345,mari#123 inside EditText1(et_tnum) . I want to Split them by Comma and After Comma i should Receive Remainder string into EditText2(et_pass). Here 12345,mari#123 is Account Number & Password Respectively.
String[] strSplit = YourString.split(",");
String str1 = strSplit[0];
String str2 = strSplit[1];
EditText1.setText(str1);
EditText2.setText(str2);
String CurrentString = "12345,mari#123";
String[] separated = CurrentString.split(",");
//If this Doesn't work please try as below
//String[] separated = CurrentString.split("\\,");
separated[0]; // this will contain "12345"
separated[1]; // this will contain "mari#123"
There are other ways to do it. For instance, you can use the StringTokenizer class (from java.util):
StringTokenizer tokens = new StringTokenizer(CurrentString, ",");
String first = tokens.nextToken();// this will contain "12345"
String second = tokens.nextToken();// this will contain "mari#123"
// in the case above I assumed the string has always that syntax (foo: bar)
// but you may want to check if there are tokens or not using the hasMoreTokens method
This answer is from this post
You can use
String[] strArr = yourString.split("\\,");
et_tnum.setText(strArr[0]);
et_pass.setText(strArr[1]);
Try
String[] data = str.split(",");
accountNumber = data[0];
password = data[1];

android stringbuilder editTexts

I have lots of editText fields and the user can add info into them.
from these editTexts i want to create one string. im using the stringBuilder at the moment. however if the user does not enter anything to some of the editTexts, i want the stringbuilder to ignore these fields. is this possible? and if so, how can i do it?
this is what im doing at the moment:
String baseString = editText1.getText().toString();
String string2= editText2.getText().toString();
String string3= editText3.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(baseString);
superStringBuilder.append(string2 + string3);
String superString = superStringBuilder.toString();
thank you
You can do something like:
If (string2.equals("")){
//Then do something when the edit text is blank.
superStringBuilder.append(string3);
} else{
superStringBuilder.append(string2 + string3);
}
Hope that helps.
thanks to your help this is an example for others if they have the same problem.
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
String string4 = editText4.getText().toString();
String string5 = editText5.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(string1);
if (string2.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string2);
}
if (string3.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string3);
}
if (string4.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string4);
}
if (string5.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string5);
}
String superString = superStringBuilder.toString();
this will make the string filter out the editText with no text in them :) so the new string created with stringbuilder is either 4 strings or 2 strings :)
thank you

Android: Divide one String into 2 String values from responseBody

With this function I got a String from the server as a response:
String responseBody = EntityUtils.toString(response.getEntity());
the String value I retrieve looks more or less like this:
"http://url.com 765889"
I want to divide the URL and the numbers into 2 String values, it should be:
String 1="http://url.com"
String 2="765889"
How am I able to perform that?
Use the split() function:
String[] parts = responseBody.split(" ");
Maybe like this
String 1 = responseBody.substring(0,responseBody.indexOf(" ")-1);
String 2 = responseBody.substring(responseBody.indexOf(" ").responseBody.length()-1);
Simples do this
StringTokenizer t = new StringTokenizer("http://url.com 765889"," ");
Log.d("first", t.nextToken());
Log.d("second", t.nextToken());

StringBuilder related issue

i have following code in which, i am fetching the data from the sqlite database. I am able to get all the values well whenever the data is upgraded but cant able to upgrade the values stored into a StringBuilder.So whenever the data is upgraded StringBuilder show the first data of the data base.
private void showData(Cursor cursor) {
StringBuilder stbuilder = new StringBuilder();
while(cursor.moveToNext())
{
start_time = cursor.getString(1);
end_time = cursor.getString(2);
duration_time = cursor.getString(3);
phone_option = cursor.getString(4);
phone_mode = cursor.getString(5);
stbuilder.append(start_time+" "+end_time+" "+duration_time+" "+phone_option+" "+phone_mode);
Log.i("STARTtttDB", ""+start_time);
Log.i("enddddDB", ""+end_time);
Log.i("duratonnn", ""+duration_time);
Log.i("OpTIONnnnn",""+phone_option);
Log.i("M O D E ",""+phone_mode);
String data=stbuilder.toString();
Log.i("Data OUtput",data);
}
You should create a new StringBuilder for each iteration, or reset it using:
stBuilder.setLength(0).
Also, instead of using string concatenation (+) inside of StringBuilder.append(), you should probably have a series of appends:
stBuilder.append(start_time);
stBuilder.append(" ");
stBuilder.append(end_time);
...
Also note that you can safely forgo the use of StringBuilder altogether as the compiler optimizes string concatenation using StringBuilder anyway. Here is a performance study I found.
You need to reset the StringBuilder with a call to setLength(0);
while(cursor.moveToNext())
{
stBuilder.setLength(0);
.....
}

Categories

Resources