permaculture playing cards*
The moose likes Beginning Java and the fly likes StringTokenizer class parsing \r\n string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer class parsing \r\n string" Watch "StringTokenizer class parsing \r\n string" New topic
Author

StringTokenizer class parsing \r\n string

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

I have following method in my class.

public static void main(String...strings )
{
StringTokenizer st = new StringTokenizer ("this\r\n virginia is\r\n a test","\\r\\n");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
I wanted to produce "this virginia is a test" as output of this program. i tried "\r\n" or "\\r\\n" as the token for this tokenizer but i am unable to produce the desired out put. can i any body tell me how to produce the out put for this.
thanks
Tanu Gulati


thanks
Tanu Gulati
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

As the following code shows, when you use \n\r as the delimiter then the string gets parsed correctly:

output:


So the problem isn't that you are tokenizing wrong, it is that by using the System.out.println() the way you were you weren't able to tell the difference. If you are hell-bent on using the tokenizer (which is legacy - use Regex as a better solution) then you would want to output or re-construct the String without the lines:



A simpler approach would be:

But you don't get the tokens. I am not sure if that is important to you. If it is, then use String#split() instead.

See the String API for more: JavaSE 6 API: String class


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Another little problem about "\r\n": That line-end in only used on Windows/DOS. *nix (and, I believe newer Mac systems) use "\n" and older Macs used "\r" as their line end. This can be found from the getProperty or getProperties methods of the String class. I can't remember the property key; it is something like line-separator but the getProperties method gives the names when you print them out. You would have to get the property as a String, split it into a char[] array, then print the chars (maybe case to int) with the %02x formatting tag; you get 0d or 0a or 0d0a. Look in an ASCII table for the values of 0a and 0d.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Tanu Gulati:
I have following method in my class.

public static void main(String...strings )
{
StringTokenizer st = new StringTokenizer ("this\r\n virginia is\r\n a test","\\r\\n");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
I wanted to produce "this virginia is a test" as output of this program. i tried "\r\n" or "\\r\\n" as the token for this tokenizer but i am unable to produce the desired out put. can i any body tell me how to produce the out put for this.
thanks
Tanu Gulati


Not quite sure what you want to do, print each line separately?


... or simply remove all line breaks from a String

?
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

yes i wanted to print each line separatly.i think Steve picked it up correctly.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Tanu Gulati:
yes i wanted to print each line separatly...


Then I recommend using the Scanner class (or as mr luke suggested: use String.split(...)) instead of the StringTokenizer.
[ October 11, 2008: Message edited by: Piet Verdriet ]
 
 
subject: StringTokenizer class parsing \r\n string
 
Similar Threads
how to know the no of occurance in a string
combining characters - \\r\\n to \r\n
JTextArea
String Tokenizer
stringTokenizer with "\\r\\n"