aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String.split() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String.split()" Watch "String.split()" New topic
Author

String.split()

Vigneshwari Chandrasekaran
Greenhorn

Joined: Jan 04, 2013
Posts: 16


While running this code, I am getting 4 as output. I thought the length will be 3.
Can anyone explain why it is 4?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18829
    
  40

Vigneshwari Chandrasekaran wrote:

While running this code, I am getting 4 as output. I thought the length will be 3.
Can anyone explain why it is 4?


Hint: if you print out what the four components are, can you decipher on how the splitting is happening?

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Vigneshwari Chandrasekaran
Greenhorn

Joined: Jan 04, 2013
Posts: 16
I tried! the output was
3 empty strings and 1 string "bb"
No clue how it is working?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

See if this gives you a clue:
Vigneshwari Chandrasekaran
Greenhorn

Joined: Jan 04, 2013
Posts: 16
yeah thank you so much!!
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
Also be careful when str = "bbaaaaaaaaa"

s.length will be 1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.split()