• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stringTokenizer with "\\r\\n"

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As already suggested in your previous post, do as the API docs recommend: use String.split(...) for this.

 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 �)
 
reply
    Bookmark Topic Watch Topic
  • New Topic