This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in StringTokenizer

 
Meet Gaurav
Ranch Hand
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am trying to split the String as (^) Separeted. Even I tryed Split method also..Any Other Way to split this one.. Please assist Me


I seen the String Tokenizer class they were charAt method for searching. This may be a reason for my problem.

Regular Expression is the only Option
[ November 03, 2008: Message edited by: Meet Gaurav ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look closely at what you are separating on; you are getting exactly what you have requested.
And please use String#split rather than StringTokenizer, as explained in the API.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Campbell Ritchie,

I was not able to split the string using Split(). Could you please assist me with the above example. I want (^) separated
 
Meet Gaurav
Ranch Hand
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tryed and finally I got this.. For the above example the beloew code splits the string to (^) separeated..

String delimeter = "\\(\\^\\)";
temp = str.split(delimeter);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was confused about what you wanted to split on.
But using StringTokenizer, if you look through the constructor details, it appears to use any part of the delimiter to split. So it can split on ( or ) or ^ or (^ or ^) or (^).
You have found out (well done ) how to get the whole pattern into a regular expression.
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting your solution for others to share!
 
Meet Gaurav
Ranch Hand
Posts: 492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to get the whole pattern into a regular expression. ??? Am not getting this one..

This is my actual Code to split the strings to "(^)" separated
( - Special Character. So i need to add "/". And the slash is also a special character. so I need to add 1 more "/". And thats it.

String[] temp = str.split("\\(\\^\\)");
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your question is here? The pattern you have provided is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic