[英]How to split a string that the user enters
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). 用户将输入存储在sqlite数据库中的权重,但我也希望该数字显示在刚刚输入该值的TextView中(因为该应用在7天的时间内跟踪着权重)。
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. 现在,上述方法会将字符串分割成单个数字,但我似乎无法弄清楚如何分割字符串,以便每两个数字分割一次。
EG User enters 20 presses enter - it then drops down into a TextView called DayOne. EG用户输入20次按Enter-然后将其拖放到名为DayOne的TextView中。
The following day the users enters 24 and presses enter- that then drops down into a TextView called DayTwo. 第二天,用户输入24并按Enter键,然后下拉至名为DayTwo的TextView中。
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? 我想我需要一个可能带有for循环的数组,但是我想知道是否可以通过调整已有的东西来实现我想要的东西?
You just mean you want to split a String every 2 characters? 您只是意味着要每2个字符分割一个字符串?
Like you have string 123456
and you want 12
, 34
, 56
? 就像你有串123456
,你想12
, 34
, 56
?
Try this: 尝试这个:
String[] split = result.split("(?<=\\\\G..)");
This is gonna split your String every 2 characters. 这将每2个字符分割一次您的字符串。 \\G
asserts the position after previous match (or the start of the String if there's not previous match) followed by 2 characters. \\G
断言上一个匹配项之后的位置(如果没有上一个匹配项,则声明字符串的开头),后跟2个字符。
[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("");
拆分字符串的部分是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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.