• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stringTokenizer with "\\r\\n"

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i apologise for my previous post related to \r\n in stringTokenizer. the actual string is "this\\r\\n virginia is\\r\\n a test" with two back slashes before "r" and "n" and i am facing the problem in parsing this.

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 the output to be like this
----------
this
virginia is
a test
---------
and hey i have found one more thing just see the subject of this closely it appears to be "\\r\\n" (double backslash before "r" and "n") present in the subject but when i see the subject in the the list of subjects in java-beginers forum it appears to be "\r\n" (single backslash now)
[ October 12, 2008: Message edited by: Tanu Gulati ]

[ October 12, 2008: Message edited by: Tanu Gulati ]
[ October 12, 2008: Message edited by: Tanu Gulati ]
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As already suggested in your previous post, do as the API docs recommend: use String.split(...) for this.

 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a StringTokenizer the defaults delimiter are as described in the API:
StringTokenizer

public StringTokenizer(String str)

Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.

Parameters:
str - a string to be parsed.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP: Here's another quote from the API docs:

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.

From: http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
[ October 12, 2008: Message edited by: Piet Verdriet ]
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one last question what is the significance of \\s* , Q and E in the regular expression.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tanu Gulati:
one last question what is the significance of \\s* , Q and E in the regular expression.


The \s means a "white space character" and the * means "zero or more times". So \s* will cause the ending and trailing white spaces before and after a \r\n to be removed. Try removing it to see what the output will be.

Since you want to split on \\r\\n, and the \ is a special character in regex, you want to tell the regex engine to NOT interpret the backslashes in your \\r\\n as special. You tell the engine that by starting with \\Q (start quote) followed by your text and ending with \\E (end quote). In short: everything between \\Q and \\E is interpreted as "normal text" by the regex engine.
[ October 13, 2008: Message edited by: Piet Verdriet ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if you're clear on the fact that ...

When
"\\r\\n"
appears as a string constant to the compiler, the backslash's are interpreted.

Here, the double backslashes are cpnverted to backslashes, so it is interpreted into the string
"\r\n"

So, if you're using a debugger at runtime to check the values of these strings, that's what you should expect.


(If you already had that under your belt, i apologize �)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic