• 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

A problem/feature with StringTokenizer

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I am sorry for the previous incomplete message. Please follow this.

This is one problem or you can call it a feature of StringTokenizer class that i would like to discuss with all. If we consider the line of code,

String testString = "test1-test2";
StringTokenizer st = new StringTokenizer(testString, "-");
while(st.hasMoreToken()) {
System.out.println(st.nextToken());
}

This will print test1 and test2.

The problem is that if we give the token as -------- (no of -) the result remains unchanged. Thus if we have the code :

StringTokenizer st = new StringTokenizer(testString, "--------");
the result will still be test1 and test2.

The reason for the same being that the token string is a AND combination rather than being an unique token in itself.

If we give the code as:

StringTokenizer st = new StringTokenizer(testString, "s1");

the result is te, t, -te, t2.

The way out to avoid the problem is using the split() method of String class. Can some one discuss the problem in more details and provide some more alternatives about tokenizing and implementation of split().

Thankz in advance.
Aniruddha.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

In constructor of StringTokenizer(stringToSearch, delimiterString) ,
every char from delimiterString could be a delimiter, not whole delimiterString, so in case of "------" delimiter could be '-' or '-' and so on.So "----" is the same as "-". delimiterString "s1" means that delimiter will be 's' or '1'.

Regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic