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
LVL 1
James CochraneB2B FINTECH WRITER/Technology WriterAsked:
Who is Participating?
 
cjjcliffordCommented:
you can use a java.io.BufferedReader (which is presumably how you did it for the file stream) instantiated with a java.lang.StringReader instead...

BufferedReader reader = new BufferedReader( new StringReader( myString ) );
reader.readLine();
0
 
zzynxSoftware engineerCommented:
>>line-by-line
You mean that a \r\n should delimit the lines?
0
 
sudhakar_koundinyaCommented:
StringTokenizer st=new StringTokenizer(string or stringbuffer.toString(),"\r\n");

Is this what you want
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
sudhakar_koundinyaCommented:
String str="Hello World!!!\r\n I am user of Experts Exchange\r\nI am a Java Programmer";
 StringTokenizer st=new StringTokenizer(str);

while(str.hasMoreElements())
String str1=st.nextElement();
0
 
zzynxSoftware engineerCommented:
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");
0
 
cjjcliffordCommented:
that way the same code you used for the file can be re-used, just change how the BufferedReader is created...
0
 
zzynxSoftware engineerCommented:
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".
0
 
zzynxSoftware engineerCommented:
The "accepted answer" is certainly the way to go
0
 
cjjcliffordCommented:
:-)
0
 
sudhakar_koundinyaCommented:
thanks for accepting
0
 
hellolyfingCommented:
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:
StringBuffer sb = new StringBuffer();
while() {
       sb.append(something)
}

Open in new window

-------
after:
ArrayList<String>  al = new ArrayList<String>();
while() {
      al.append(someting +  "\n\r")
}

Open in new window

and just return the ArrayList instance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.