Solved

Reading from a string or stringbuffer

Posted on 2004-10-13
11
1,210 Views
Last Modified: 2012-05-21
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
0
Comment
Question by:techhound
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12297276
>>line-by-line
You mean that a \r\n should delimit the lines?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12297301
StringTokenizer st=new StringTokenizer(string or stringbuffer.toString(),"\r\n");

Is this what you want
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 200 total points
ID: 12297313
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
 
LVL 11

Accepted Solution

by:
cjjclifford earned 300 total points
ID: 12297352
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12297356
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:cjjclifford
ID: 12297363
that way the same code you used for the file can be re-used, just change how the BufferedReader is created...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12297491
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12297504
The "accepted answer" is certainly the way to go
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12297908
:-)
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12297965
thanks for accepting
0
 

Expert Comment

by:hellolyfing
ID: 37995107
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
Java Timer (static) 9 36
expectj telnet failing 5 26
use lov values 2 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now