我如何拆分一个字符串换行?字符串、换行

2023-09-06 14:42:03 作者:lōvé甜心ぺ

我是小白到Android的发展,我想通过它的多种换行符拆分字符串多次。我试图分裂字符串是从数据库查询和拉这样的构造:

I'm a noob to android development and I am trying to split a string multiple times by its multiple line breaks. the string I'm trying to split is pulled from a database query and is constructed like this:

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内容如下:

result.getCoin reads as this:

alphabravocharlie

我要分割字符串的换行,并把每个子成一个字符串数组。这是我目前的code:

I want to split the string at the line break and place each substring into a String Array. This is my current code:

String[] separated = result.split("\n");
      for (int i = 0; i < separated.length; i++) {
           chartnames.add("$." + separated[i] + " some text" ); 
           }

这给我的输出:

"$.alpha
bravo
charlie some text"

而不是我所希望的输出:

instead of my desired output of:

"$.alpha some text, $.bravo some text, $.charlie some text"

任何帮助是极大AP preciated

Any help is greatly appreciated

推荐答案

您可以通过换行符使用以下的语句拆分的字符串:

you can split a string by line break by using the following statement :

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