oOOMIAO
2012-11-20 02:54
采纳率: 66%
浏览 19.0k

如何换行分割字符串???

我希望通过换行分割字符串,字符串是来自一个数据库,结构如下:

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"

谢谢帮忙了。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • O_1001_O 2012-11-20 03:22
    已采纳

    用这个语句:

    String lines[] = String.split("\\r?\\n");
    
    点赞 打赏 评论
  • ce_la_vie 2012-11-20 03:11

    用标准的I/O类解决,有点大材小用:

    BufferedReader rdr = new BufferedReader(new StringReader(result));
    List<String> lines = new ArrayList<String>();
    for (String line = rdr.readLine(); line != null; line = rdr.readLine()) {
        lines.add(line);
    }
    rdr.close();
    
    点赞 打赏 评论
  • ipple1986 2017-11-27 08:31

    String lines[] = String.split("\r?\n");

    点赞 打赏 评论

相关推荐 更多相似问题