简体   繁体   中英

How can I read line after line from big String?

Consider I have some big String in the following structure:

parameter1|parameter2|parameter3|parameter4+"\0"

*Where parameter is sequence of some chars in a generally unknown length.

Now consider my big String is holding 4 lines of data, for example:

parameter1|parameter2|parameter3|parameter4+"\0"
parameter1|parameter2|parameter3|parameter4+"\0"
parameter1|parameter2|parameter3|parameter4+"\0"
parameter1|parameter2|parameter3|parameter4+"\0"

How can I read the first line and then read the second line and so on?
I know that I can use Substring method to get the first line every time easily, however
how can I use Substring (or other methods) to get the next 3 lines of the big string, in order to extract the first line again (which represent the second line of the original big string in practice).
I know that all I need is the index of the last "\\0" (line 4) and I already have the index of current line "\\0".

Should I just use lastIndexOf("\\0") ?
If you have any other ideas or even criticism about this way of working, I'll be more than glad to hear about.

Thanks :)

You can use the StringReader wrapped into a BufferedReader, something like:

BufferedReader r = new BufferedReader(new StringReader("my multiline text..."));
String l = null;
while ((l=r.readLine)!=null) {
 ....
 // each line in "l"
}

http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine() http://docs.oracle.com/javase/6/docs/api/java/io/StringReader.html

You could use the String.split method with a "\\0" argument. You will then get an array of strings ( String[] ) which you can then do whatever you wish with :)

Perhaps,

inputString.split("\0");

?

I would use regular expression to separate the string. In case you want all words in one string array you can just use /s (whitespace) as a separator pattern.

You can find information here http://www.regular-expressions.info/java.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM