• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split method in java

 
Nev Mehta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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'
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//String[] tokens = nnEntry.split("|");
String[] tokens = nnEntry.split("\\|");
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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!
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such character as ''.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic