【问题标题】:Java Android - IndexOf &OffsetJava Android - IndexOf &Offset
【发布时间】:2013-01-25 09:38:22
【问题描述】:

我正在尝试从文本文件中读取,但这些字符串分成不同的属性,但我不知道在第一次拆分后如何进行。

这是我的代码:getType() 字符串的偏移量应该是多少?

try {
        InputStream is = context.getAssets().open("Autoeval");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         //Skips lines
        for (int i = 0; i< questionNumber; i++) {
            reader.readLine();
        }

        question = reader.readLine();

    } catch (IOException e) {
        e.printStackTrace();
    }
}



public String getId() {

    return question.substring(0, question.indexOf(";"));
}
public String getType() {
    return question.substring(question.indexOf(";"));
}

【问题讨论】:

    标签: substring indexof


    【解决方案1】:

    ,但你为什么不创建2个全局私有变量:

    private String _id;
    private String _type;
    

    然后,在您阅读问题后,您可以这样做:

    {
        //stuff
    
        question = reader.readLine();
    
        _id = question.substring(0, question.indexOf(";"));
        _type = question.substring(_id.length); // assuming no other ";" delimiters
    
    }
    
    public String getId() {
        return _id;
    }
    
    public String getType() {
        return _type;
    }
    

    说了这么多,还有更好的方法来做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多