• 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

String split question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I have a line of data in a txt file that I need to read in and apply some basic business rules too. I have used the stringtokenizer to split up the line, but I read somewhere that better performance can be reached if I split it up, but I can't get it to work?
Below is the line of text:
16|arthur|6006477|1810324||M|01011800|828300|003|30|285.33|0
The delimiter is a pipe |.
I have tried both these commands, but my array still doesn't contain the token between the pipes.
1. "Pattern pattern = Pattern.compile( String.valueOf('|') );
String[] splits = pattern.split( lineOfData );"
2. "String[] splits = lineOfData.split("|");"
lineOfData is the pipe delimited line that I have read in.
And advise would be greatly appreciated.
Many thanks
AJ
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that String.valueOf('|') wasn't the first thing you tried - "|" is much more straightforward. But maybe when that didn't work you experimented with other more convoluted options out of desperation? That would make sense. Anyway, the problem here is that | is a special character in regular expressions (indicating "or", as in "foo|bar" will match "foo" or "bar". "|" matches [empty string] or [empty string] - and you can find empty strings between any two chars in your input, which is why the split() returns each character as a separate field. In order to use | as if it were a normal character, you must escape it with \\ - one \ for the regex parser, and one more \ to escape the other \ for the Java compiler. :roll: . Weird I know. Reasonable complete docs for regular expressions can be found at java.util.regex.Pattern.
[ May 04, 2003: Message edited by: Jim Yingst ]
 
Arthur Jenkins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim
Thanks for your reply, which sorted out the problem 100%.
Many thanks for your help.
AJ
 
reply
    Bookmark Topic Watch Topic
  • New Topic