wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes split() method 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "split() method" Watch "split() method" New topic
Author

split() method

binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14
hello everyone,
i'm new to this forum and i'm preparing for ocpjp6
can anyone explain me the working of the split() method in the code below? I'm confused about the empty strings that it returns.
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

Check out this link for details about split() Java Docs
and for class pattern and regular expressions JavaDocs

Hope this helps!


Always believe in yourself
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Hi Binay. Welcome to the Ranch!

What were you expecting it to print out? \w means "any word character" (letter, digit, underscore). So what you're doing there is roughly the equivalent of splitting the string ",, ,, , #" on commas. Can you see now why it would include empty strings?
binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14
I was expecting it to print "> <> <> #" without the empty string.From where this empty string comes from.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

The empty string comes because you've got tokens you are splitting on (like 'x' and '1') that have no characters between them. Similarly, my example has consecutive commas - this will cause an empty string when you split on commas.
binay shah
Greenhorn

Joined: Jan 19, 2013
Posts: 14
thanks. Got it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: split() method
 
Similar Threads
variable initialisation
Doubt in split().
Passing a String reference to a method.
Simple thread question
Alternative to string tokenzier