This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 4240
    
    7

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: 4240
    
    7

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split() method
 
Similar Threads
Doubt in split().
variable initialisation
Passing a String reference to a method.
Simple thread question
Alternative to string tokenzier