This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes split method in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "split method in java" Watch "split method in java" New topic
Author

split method in java

Nev Mehta
Greenhorn

Joined: Jan 09, 2007
Posts: 26
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

Joined: Jun 09, 2003
Posts: 4632
//String[] tokens = nnEntry.split("|");
String[] tokens = nnEntry.split("\\|");
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
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

Joined: Jan 09, 2007
Posts: 26
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

Joined: Jun 09, 2003
Posts: 4632
> ...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

Joined: Oct 13, 2005
Posts: 38044
    
  22
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

There is no such character as ''.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split method in java
 
Similar Threads
Adding 2-d ArrayList to JTable
Int array to 3D array
REGEX Help
Tokenising using String.split().
Regular Expressions on a .csv file