public class LineSplitter extends java.lang.Object implements java.util.Iterator<java.lang.String>, java.lang.Iterable<java.lang.String>
Iterator
. The
default setting is to not return trailing empty lines. Use
setIncludeTrailingEmptyLine(boolean)
to include them.
Note: According to tests I performed this is the fastest method to split a string. It is about nine times faster than the regex-bases split with:
Pattern pattern = Pattern.compile("\r\n|\r|\n"); pattern.split(content);
Constructor and Description |
---|
LineSplitter()
Constructor for empty content.
|
LineSplitter(java.lang.String content)
Constructor which calls
setContent(String) . |
Modifier and Type | Method and Description |
---|---|
boolean |
hasNext() |
java.util.Iterator<java.lang.String> |
iterator() |
java.lang.String |
next()
Obtain next identified line.
|
void |
remove() |
void |
setContent(java.lang.String content)
Set the string to split and reset the iterator.
|
void |
setIncludeTrailingEmptyLine(boolean includeTrailingEmptyLine)
Enables returning of trailing empty lines during the iteration.
|
public LineSplitter()
public LineSplitter(java.lang.String content)
setContent(String)
.public void setContent(java.lang.String content)
content
- The string to split. If string is null
or the empty
string, next()
will return null
.public boolean hasNext()
hasNext
in interface java.util.Iterator<java.lang.String>
public java.lang.String next()
next
in interface java.util.Iterator<java.lang.String>
null
if all lines were returned. On returning the last
line all references to the input string are deleted. So it is free
for garbage collection.public void setIncludeTrailingEmptyLine(boolean includeTrailingEmptyLine)
false
If true
the string Foo\nBar\n
will yield three items
(Foo, Bar and the empty string), otherwise two items (Foo and Bar).
public void remove()
remove
in interface java.util.Iterator<java.lang.String>
public java.util.Iterator<java.lang.String> iterator()
iterator
in interface java.lang.Iterable<java.lang.String>