Home > Archive > Java Help > May 2006 > split a text in lines: how?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
split a text in lines: how?
|
|
| mario.lat_ 2006-05-24, 7:07 pm |
| Hallo!
I have a function that give me a String.
In this string there is a long text.
How can transform this text in a vector of Strings, one for each lines?
Thank you in advance for the time you'll spend for answer me.
Mario.
| |
| Oliver Wong 2006-05-24, 7:07 pm |
|
"mario.lat_" <mario.lat@libero.it> wrote in message
news:pan.2006.05.24.16.32.46.428341@libero.it...
> Hallo!
> I have a function that give me a String.
> In this string there is a long text.
> How can transform this text in a vector of Strings, one for each lines?
http://java.sun.com/j2se/1.5.0/docs...ring.html#split(java.lang.String)
- Oliver
| |
|
|
"mario.lat_" <mario.lat@libero.it> wrote in message
news:pan.2006.05.24.16.32.46.428341@libero.it...
> Hallo!
> I have a function that give me a String.
> In this string there is a long text.
> How can transform this text in a vector of Strings, one for each lines?
>
> Thank you in advance for the time you'll spend for answer me.
> Mario.
The obvious way to split a String into parts is with a StringTokenizer.
I'm assuming that your function puts line break characters, usually "\n" but
sometimes "\n\r" (or "\r\n"?), in the String at the appropriate places in
the text. If you set the delimiter for the StringTokenizer to the line break
characters, the tokenizer should have no trouble splitting the String into
individual lines. Have a look at the StringTokenizer class in the API.
Here is a short example that reads a String containing line breaks,
tokenizes it so that each line is a separate token, then stores each token
in a Vector and displays that Vector:
String myString = "Row, row, row your boat\nGently down the stream\nMerrily,
merrily, merrily, merrily\nLife is but a dream!";
StringTokenizer stringTokenizer = new StringTokenizer(myString, "\n");
System.out.println("The String contains " + stringTokenizer.countTokens() +
" lines."); //Display the number of lines in the String
Vector<String> stringVector = new Vector<String>();
while (stringTokenizer.hasMoreTokens()) { //Store each line as a separate
element of the vector
stringVector.add(stringTokenizer.nextToken());
}
for (String oneLine : stringVector) { //Display the contents of the vector,
one line at a time
System.out.println(oneLine);
}
If that approach doesn't appeal to you, there are other possibilities, like
the split() method of the String class. That approach requires you to learn
Regular Expressions, which are widely used in Java, but that is a little
more work than the approach I've suggested above.
--
Rhino
| |
| Dale King 2006-05-25, 7:06 pm |
| Rhino wrote:
> "mario.lat_" <mario.lat@libero.it> wrote in message
> news:pan.2006.05.24.16.32.46.428341@libero.it...
>
> The obvious way to split a String into parts is with a StringTokenizer.
Obvious or not, it is best to avoid StringTokenizer. Sun even
discourages it (though they haven't deprecated it yet).
From the javadocs for StringTokenizer: "StringTokenizer is a legacy
class that is retained for compatibility reasons although its use is
discouraged in new code. It is recommended that anyone s ing this
functionality use the split method of String or the java.util.regex
package instead."
There are good reasons to avoid it. They botched the implementation.
They actually changed the behavior in different versions of the JDK so
that you get different results depending on which JVM you are running
your code on. I got bit by that one myself.
See:
http://bugs.sun.com/bugdatabase/vie...?bug_id=4238266
http://bugs.sun.com/bugdatabase/vie...?bug_id=4338282
--
Dale King
| |
|
|
"Dale King" <"DaleWKing [at]gmail [dot] com"> wrote in message
news:tOadnRUko-UeJ-jZnZ2dnUVZ_sCdnZ2d@insightbb.com...
> Rhino wrote:
>
> Obvious or not, it is best to avoid StringTokenizer. Sun even discourages
> it (though they haven't deprecated it yet).
>
> From the javadocs for StringTokenizer: "StringTokenizer is a legacy class
> that is retained for compatibility reasons although its use is discouraged
> in new code. It is recommended that anyone s ing this functionality use
> the split method of String or the java.util.regex package instead."
>
> There are good reasons to avoid it. They botched the implementation. They
> actually changed the behavior in different versions of the JDK so that you
> get different results depending on which JVM you are running your code on.
> I got bit by that one myself.
>
> See:
>
> http://bugs.sun.com/bugdatabase/vie...?bug_id=4238266
> http://bugs.sun.com/bugdatabase/vie...?bug_id=4338282
>
Thanks for the warning, Dale! I haven't been bitten myself so I didn't
realize that StringTokenizer was a poor choice. I'll bear that in mind for
future code. I may even revise my old code to use the String.split() method!
It would probably be good for me to use REGEX more :-)
--
Rhino
| |
| Roedy Green 2006-05-26, 7:07 pm |
| On Wed, 24 May 2006 18:32:47 +0200, "mario.lat_" <mario.lat@libero.it>
wrote, quoted or indirectly quoted someone who said :
>I have a function that give me a String.
>In this string there is a long text.
>How can transform this text in a vector of Strings, one for each lines?
The shortest code is with a regex split. See
http://mindprod.com/jgloss/regex.html
If the data are in a file, you might use a BufferedReader with
readLine.
See http://mindprod.com/applets/fileio.html
for sample code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
|
|
|