• 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

Problem in StringTokenizer

 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for posting your solution for others to share!
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what your question is here? The pattern you have provided is correct.
 
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic