• 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

Pattern matching

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a string lets say "ABC@^*NNNNN@^*sdsdsds@^*dsdsds"
I have to split this string matching @^*. I can use String Tokenizer to split it.
But if the string is "AB*C@^*NNNNN@^*sdsdsds@^*dsdsds" (note the *), it fails.I need the result as

AB*C
NNNNN
sdsdsds
dsdsds

How do I do it....Pls advice
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any luck guys
[ July 14, 2006: Message edited by: Mary Cole ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Mary]: I have to split this string matching @^*. I can use String Tokenizer to split it.

No, StringTokenizer doesn't work the way you think it does. If you tell StringTokenizer to use "@^*" for the delimiter, that means it will use @, ^, or * as delimiters. There's no way to tell StringTokenizer to use the whole string as a single delimiter - it's always a list of possible single-character delimiters. This is typically not what users expect, and that's one reason by StringTokenizer is not widely recommended these days. An alternative is to use the split() method in String (available since JDK 1.4). Unfortunately this has some issues too, as you need to know about regular expressions to use this correctly. The problem is that ^ and * have special meaning in regular expressions, and to refer to literal ^ and *, you need special escape sequences. Here are three options:



Or if regular expressions aren't to your taste, you could just use String's indexOf() method to find the position of each @^*, then use substring() to extract the other bits.
[ July 14, 2006: Message edited by: Jim Yingst ]
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic