• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer class parsing \r\n string

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i wanted to print each line separatly.i think Steve picked it up correctly.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic