I have a file with lines of text like this:
[A]
This is one line.
This is another line.
[B]
A third line.
…
and so forth. I want to read this file into Java and look for the lines which only contain [A] etc. for further reference. I tried:
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.texts);
byte[] b = new byte[in_s.available()];
in_s.read(b);
String textstring = new String(b);
String[] textarr = textstring.split("[\\r\\n]+");
And then:
int lineB = 0;
for (int i=0; i<textarr.length; i++) {
if textarr[i].substring(0, 3) == "[B]") lineB = i;
}
Afterwards, line is still zero. First I thought this has something to do with how new lines are handled (I’m using Windows), but I also had no luck with substring(0,3). I want this to give me lineB = 3, any ideas what I’m doing wrong?
Use String#startsWith like this: