Reputation: 730
Ok so I have looked at this (question asking how to split a string) however the answer isn't really relevant to my question.
The user will input a weight which is stored in the sqlite DB but I also want the number to show in a TextView below where it was just entered (as the app keeps track of weights over a period of 7 days).
When I try and get the String from my DB its stored as a long String and what I want to do is split that String (I hope I'm making sense!).
I have the following method;
public String[] getWeight() {
String selectQuery = "SELECT " + DowncroftContract.WEIGHT_VALUE + " FROM " + DowncroftDatabase.WEIGHT_TABLE; //+ " WHERE " + DowncroftContract.WEIGHT_DOGS_ID + " = " + str_dogsId + " ORDER BY " + DowncroftContract.WEIGHT_DATE + " ASC;";
SQLiteDatabase db = downcroftDatabase.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
String[] data = null;
if (cursor.moveToFirst()) {
do {
results = cursor.getString(cursor.getColumnIndex(DowncroftContract.WEIGHT_VALUE + ""));
String[] splitString = results.split("");
String split1 = splitString[1];
String split2 = splitString[2];
DayOne.append(split1);
DayTwo.append(split2);
} while (cursor.moveToNext());
}
cursor.close();
return data;
}
Now the above method will split the string up to single figures but I cant seem to figure out how to split the string so that its splitting it for every two figures.
E.G User enters 20 presses enter - it then drops down into a TextView called DayOne.
The following day the users enters 24 and presses enter- that then drops down into a TextView called DayTwo.
I think I need an array with possibly a for loop however I am wondering if it is possible to achieve what I want by tweaking what I already have?
Upvotes: 0
Views: 92
Reputation: 23
[Guy above did basically the same thing, only saw on refresh, snippet will probably still be useful though..]
The part that is splitting your string is String[] splitString = results.split("");
This splits after every "nothing", so in essence, after every character split the string.
Here's a little code snippet I worked together for you...
import java.util.Arrays; //only essential for the Arrays toString bit...
public class Main {
//Static so it's usable anywhere..
public static String[] splitStringBy(int everyXLetter, String stringCode)
{
// Split the given string by regex, every Xth letter...
String[] splitIntoSingleElements = stringCode.split("(?<=\\G.{"+everyXLetter+"})");
// Return it
return splitIntoSingleElements;
}
public static void main(String[] args)
{
//Set some string text...
String text = "askfjaskfjasf";
//Split and store the string using the above function
String[] splitText = splitStringBy(2, text);
//Return it any way you like
System.out.println(Arrays.toString(splitText)); // [as, kf, ja, sk, fj, as, f]
System.out.println(splitText[0]); // as
}
}
Upvotes: 0
Reputation: 5592
You just mean you want to split a String every 2 characters?
Like you have string 123456
and you want 12
, 34
, 56
?
Try this:
String[] split = result.split("(?<=\\G..)");
This is gonna split your String every 2 characters. \G
asserts the position after previous match (or the start of the String if there's not previous match) followed by 2 characters.
Upvotes: 1