Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2007, 07:10 PM
Member
 
Join Date: Nov 2007
Posts: 97
Rep Power: 0
javaplus is on a distinguished road
Default How to use StringTokenizer for multiple tokens
Here is the situation:

I have a String and I want to get tokens from it. I have 3 delimiters which are:
" " (space)
"," (comma)
";" (semi colon)

Is there a way to use StringTokenizer with these 3 demiliters?

Please look into this.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-28-2007, 08:22 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Code:
String s = "The StringTokenizer methods do not distinguish " +
           "among identifiers, numbers, and quoted strings, " +
           "nor do they recognize and skip comments; it is " +
           "a legacy class.";
System.out.println("s = " + s);
String delims = " ,;";
StringTokenizer tokens = new StringTokenizer(s, delims);
while(tokens.hasMoreTokens())
    System.out.println(tokens.nextToken());
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-29-2007, 10:38 AM
Member
 
Join Date: Nov 2007
Posts: 97
Rep Power: 0
javaplus is on a distinguished road
Default
Perfect. It worked. Thanks.

I have another question. What if my token is composed of more than one characters?

Code:
String s= "I am new to Java. Are you good in Java, if yes please help me.";
String delim = "Java";
StringTokenizer tokens = new StringTokenizer(s, delim);
while(tokens.hasMoreTokens())
    System.out.println(tokens.nextToken());
Output:
Code:
Welcome
I 
m new to 
. Are you good in 
, if yes ple
se help me.
Actually "Java" is not treated as a token but 4 different tokens (each character is a token). How to handle this?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting tokens using Scanner class Java Tip Java Tips 0 02-05-2008 10:11 AM
tokens Gilgamesh New To Java 5 12-03-2007 12:30 AM
tokens Gilgamesh New To Java 3 11-25-2007 03:39 AM
StringTokenizer Java Tip Java Tips 0 11-08-2007 09:48 AM
StringTokenizer Java Tip Java Tips 0 11-03-2007 10:24 PM


All times are GMT +2. The time now is 01:26 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org