File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes stringTokenizer with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stringTokenizer with "\\r\\n"" Watch "stringTokenizer with "\\r\\n"" New topic
Author

stringTokenizer with "\\r\\n"

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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 ]

thanks
Tanu Gulati
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
As already suggested in your previous post, do as the API docs recommend: use String.split(...) for this.

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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.


Regards, Rene Larsen
Dropbox Invite
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
@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

Joined: Oct 06, 2008
Posts: 113

one last question what is the significance of \\s* , Q and E in the regular expression.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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

Joined: Nov 08, 2007
Posts: 457
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 �)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stringTokenizer with "\\r\\n"