我是android开发的菜鸟,我试图通过它的多个换行符多次拆分一个字符串。我试图拆分的字符串是从数据库查询中提取的,并且构造如下:
public String getCoin() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iName).substring(0, Math.min(18, c.getString(iName).length())) + "\n";
}
c.close();
return result;
result.getCoin 是这样写的:
alphabravocharlie
我想在换行符处拆分字符串并将每个子字符串放入字符串数组中。这是我当前的代码:
String[] separated = result.split("\n");
for (int i = 0; i < separated.length; i++) {
chartnames.add("$." + separated[i] + " some text" );
}
这给了我一个输出:
"$.alpha
bravo
charlie some text"
而不是我想要的输出:
"$.alpha some text, $.bravo some text, $.charlie some text"
任何帮助是极大的赞赏