Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split() method

 
binay shah
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this link for details about split() Java Docs
and for class pattern and regular expressions JavaDocs

Hope this helps!
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was expecting it to print "> <> <> #" without the empty string.From where this empty string comes from.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. Got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic