• 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

split method in java

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

String nnEntry = "1|Some_123F_N45-3568_1111098908|1|1|0"
String[] tokens = nnEntry.split("|");

tokens ends up looking like this:
[, 1, |, S, o, m, e, _, 1, 2, 3, F, _, N, 4, 5, -, 3, 5, 6, 8, _, 1, 1, 1, 1, 0, 9, 8, 9, 0, 8, |, 1, |, 1, |, 0]

whereas i need it to look like;
[1 , Some_123F_N45-3568_1111098908, 1, 1, 0]

No idea why thats happening;

Thanks'
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//String[] tokens = nnEntry.split("|");
String[] tokens = nnEntry.split("\\|");
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit more explanation is called for. If you go through the Java™ Tutorials section, you find that | is a "meta-character" for "OR" which means you are splitting on what is before the | or what is after the |. So in this instance you are splitting on "nothing" or "nothing". After each letter the application finds"nothing" so it splits there.
 
Nev Mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know if i'm thinking correct but shouldn't splitting on 'nothing' mean the entire string becomes one token. Wouldnt this always be false (where NOTHING is empty string)?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> ...but shouldn't splitting on 'nothing' mean the entire string becomes one token.

the command is to 'split' the string.
with no parameters (or 'nothing'), the string will be split into individual characters
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nev Mehta wrote:Don't know if i'm thinking correct but shouldn't splitting on 'nothing' mean the entire string becomes one token. Wouldnt this always be false (where NOTHING is empty string)?

No, it means "if you find nothing after this character and before the next." And there is always "nothing" between character 1 and character 2!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no such character as ''.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic