Can I read String Builder line by line? And Get the length of each line as well.
EDIT:
"I build string in StringBuilder and add "\n" within. And I need to read 开发者_开发问答it again. I need to consider that every "\n" has a new line."
Given your edit, it's as simple as invoking toString()
on the StringBuilder
instance, and then invoking split("\\n")
on the returned String
instance. And from there, you'll have a String
array that you can loop through to access each "line" of the StringBuilder
instance. And of course, invoke length()
on each String
instance, or "line" to get its length.
StringBuilder sb = new StringBuilder();
sb.append("line 1");
sb.append("\\n");
sb.append("line 2");
String[] lines = sb.toString().split("\\n");
for(String s: lines){
System.out.println("Content = " + s);
System.out.println("Length = " + s.length());
}
Make a Scanner object and there you can easily parse line for line and get the length.
Scanner scan = new Scanner(sb.toString()); // I have named your StringBuilder object sb
while (scan.hasNextLine() ){
String oneLine = scan.nextLine();
System.out.println(oneLine.length());
}
As already answered, in the simple case you can just use toString().split()
. If you want something a bit more flexible and which also supports line removal, it's pretty straightforward to iterate over the lines by just keeping track of the start and end values of the last line that you returned:
new Iterator<String>() {
boolean hasNext = true;
boolean removed = false;
int start = 0;
int end = -1;
@Override public boolean hasNext() {
return hasNext;
}
@Override public String next() {
removed = false;
if (!hasNext) {
throw new NoSuchElementException();
}
start = end + 1;
end = value.indexOf("\n", start);
if (end == -1) {
hasNext = false;
}
return value.substring(
start,
end == -1 ? value.length() : end);
}
@Override public void remove() {
if (removed) {
throw new IllegalStateException("Multiple calls to remove() without and intervening call to next().");
}
value.delete(
start,
end == -1 ? value.length() : end + 1);
end = start - 1;
removed = true;
}
};
You can see this in a utility class that returns a LineIterator for String or StringBuilder here: http://pastebin.com/JA6abTg3
And it has tests which show how it can be used here: http://pastebin.com/X1b5cGX8
Not sure what you're trying to do, but you can call toString().split()
on it to get an array. You can split on line delimiters.
I think there no method to read stringbuilder line by line. but if you want to separate whole string by line than you may ad newline character ""\n"" when you append string in StringBuilder.
Use ("\n");, when you appending to stringbuilder. This is the simple way of doing.
I realize I'm replying several years later but ran into the same problem.
Instead of playing these games with Split(), etc. why not just build a list of strings?
Dim Lines = New List(of String)
Dim Length As Integer
Lines.Add("The quick brown fox jumped over the lazy dog's back.")
Lines.Add("The slow turtle crawled down the grassy lane.")
For Each Line in Lines
Length = Line.Length
Debug.WriteLine("Length = " + Length)
Next
精彩评论