• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringTokenizer I want to pass a String and tokenize  RSS feed

 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to pass a String and tokenize.But I also want to specify the delimiters.My problem is how to specify symbols like ".

Specifying the String to be tokenized is also a problem to me


Varuna
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the JavaDoc for the StringTokenizer class, it states:


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.


What you want to do is much easier to do using the String.split(String regex) method. Since you want to split on a dot/period, and that character has special meaning in a regular expression (regex), you need to escape it. You then need to escape the escape characters when you put it into a quoted String. So you end up getting:




[edit]

After rereading your post, it looks like you may want to split on a quote. I thought you were quoting a period, but you had a quote at the end of the sentence and the period was ending the sentence. Anyhow, to split on a double quote, it is the same thing. You do not need to escape the " in the regex, but you do need to do so for the String that represents the regex:



This would result in an array of:


Notice the starting empty String, the single space tokens between the letters . If you want to get the stuff just between the quotes, you can check each token to make sure it is not an empty String (i.e. "") or spaces. The best thing to do would be to trim() the result token and then make sure it is not an empty String. There is another way to do this with a fancier regex and some of the classes in the java.util.regex library. Doing so would eliminate the empty Strings and tokens of spaces.

Is that what you are trying to do, get the words between quotes?

Originally posted by Varuna Seneviratna:
Specifying the String to be tokenized is also a problem to me

Remember that you have to escape a quote that is inside a String:


[ December 08, 2008: Message edited by: Mark Vedder ]
 
Varuna Seneviratna
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to escape the " in the regex, but you do need to do so for the String that represents the regex:






What do you mean from the above statement?

Varuna
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Varuna Seneviratna:

What do you mean from the above statement?

Varuna


The double quote does not have special meaning in a regular expression. So it does not need to be escaped as far as the regular expression is concerned. But because it has special meaning in Java, it does need to be escaped.

Are you familiar with what it means to escape something and what an escape character (or an escape sequence) is?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!