【问题标题】:StringBuilder to Array of Strings how to convertStringBuilder到字符串数组如何转换
【发布时间】:2014-11-06 20:25:36
【问题描述】:

无法找到或阅读有关此问题的任何内容(这只是我的问题,但也许有人知道解决它的方法。

我有从资产文件夹读取的功能

public String[] loadFromAsset() throws IOException
    {
        String TEMPBUFFER = null;
        String[] temp;
        temp = new String[60];
        int tempc = 0;
        BufferedReader bReader = new BufferedReader(new InputStreamReader(cont.getAssets().open("myquests.txt")));
        String line="";//  = bReader.readLine();
        StringBuilder sb = new StringBuilder();
        while (line != null) {

            line = bReader.readLine();
            sb.append(line).append("TABTAB");
            tempc++;
  }
         bReader.close();
    //convert SB to array here
         saveCount(tempc-1);
         return temp;

    }

需要返回字符串数组,例如 temp[50] = (1,2,3,4,5,...n+1) 但我找不到从 stringBuilder 转换为数组的方法:( 拜托,也许有人知道。告诉我

只需要将 SB 作为数组返回(如 temp[])或将 SB 转换为 Array,如 sb->convert->temp[]

【问题讨论】:

  • 那你为什么首先使用stringbuilder呢?
  • 您需要转换 temp 吗?
  • 需要使用它将 StringBuilder 转换为 temp(字符串数组),因为它是最快的方式
  • 它比什么更快?
  • sb 比我使用时更好更快: temp[tempc]=line; // 不知道为什么 - 但它比我使用 stringbuilder 需要更多时间

标签: android arrays string stringbuilder


【解决方案1】:

要将 stringbuilder 转换为字符串数组,请执行以下操作:

string sbString = sb.toString();
String[] ary = sbString.split("TABTAB");

【讨论】:

    【解决方案2】:

    temp = sb.toString().split("TABTAB"); 将拆分字符串并为每一行返回一个字符串数组。但我不完全确定这就是你想要做的......

    【讨论】:

    • 谢谢你。也试过了,它的工作,去这里但你已经回答了,我只是写“return sb.toString().split("TABTAB");
    • 好点。如果您只想返回结果,则不需要 temp 变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    相关资源
    最近更新 更多