James Cochrane
asked on
Reading from a string or stringbuffer
I am successfully able to read line-by-line from a file stream. Is there a way to do the same thing using a string or a stringbuffer? In other words, if I had information that was passed in from an HTML <TEXTAREA> and it was multiple lines, is there a way in Java to read that line-by-line?
Thanks
Thanks
StringTokenizer st=new StringTokenizer(string or stringbuffer.toString(),"\ r\n");
Is this what you want
Is this what you want
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead
That's why this is better:
String originalString = "Line1\r\nLine2";
String lines[] = originalString.split("\r\n ");
It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead
That's why this is better:
String originalString = "Line1\r\nLine2";
String lines[] = originalString.split("\r\n
that way the same code you used for the file can be re-used, just change how the BufferedReader is created...
Hi techhound,
If you consider as assisted answer a solution that uses a class which use is discouraged,
I feel like my comment also deserved the label "assisted answer".
If you consider as assisted answer a solution that uses a class which use is discouraged,
I feel like my comment also deserved the label "assisted answer".
The "accepted answer" is certainly the way to go
:-)
thanks for accepting
Some days ago.I met the same question.
Until last night, I suddenly realized that StringBuffer is just a mid-parameter,why don't we connect the source String with the destination string-collector directly?!
like this:
------
before:
after:
Until last night, I suddenly realized that StringBuffer is just a mid-parameter,why don't we connect the source String with the destination string-collector directly?!
like this:
------
before:
StringBuffer sb = new StringBuffer();
while() {
sb.append(something)
}
-------after:
ArrayList<String> al = new ArrayList<String>();
while() {
al.append(someting + "\n\r")
}
and just return the ArrayList instance.
You mean that a \r\n should delimit the lines?