我希望通過換行分割字符串,字符串是來自一個數據庫,結構如下:
public String getCoin() {
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"
謝謝幫忙了。。
用這個語句:
String lines[] = String.split("\\r?\\n");