File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Alternative to StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternative to StringTokenizer " Watch "Alternative to StringTokenizer " New topic

Alternative to StringTokenizer

Bhushan Shete

Joined: Mar 20, 2001
Posts: 9
Is there any alternative ( other than substring ) that can be used instead of StringTokenizer ???
Actually, I am facing a problem with StringTokenizer. If I have a string with a character '=' in it, then the following code results in false tokens :
StringTokenizer valToken = new StringTokenizer(val,"==>");
valno = valToken.nextToken();
valdate= valToken.nextToken();

Frank Carver

Joined: Jan 07, 1999
Posts: 6920
This is not really a servlets and JSP question, so I've moved it to Java in General (intermediate)

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
When you pass a second (String) argument to StringTokenizer, the characters in the String are considered individually, not together. Meaning it will not look for the pattern "==>" but rather for an individual '=' or '>'. Having two of the same character in the String is redundant, but not an error. If what you really want is to find the pattern "==>" but not lone '=' and '>' chars, use the indexOf() method in String:
int pos = val.indexOf("===>");
if (pos < 0) {<br /> System.out.println("Not found");<br /> }<br /> else {<br /> System.out.println("Before: " + val.substring(0, pos));<br /> System.out.println("After: " + val.substring(pos + 3));<br /> }<br /> </pre></code><br /> To get more of the behavior of StringBuffer, you may want to construct a loop that will look for successive occurrences of the "==>" substring.

"I'm not back." - Bill Harding, Twister
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
And now I see you've posted the same question in Java in General (Advanced), so I'm closing this thread. Please don't waste people's time by posting the same question in multiple forums; I hate to spend time answering a question that's already been done.
I agree. Here's the link:
subject: Alternative to StringTokenizer
jQuery in Action, 3rd edition