Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.split()

 
Vigneshwari Chandrasekaran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Marshal
Pie
Posts: 21391
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vigneshwari Chandrasekaran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried! the output was
3 empty strings and 1 string "bb"
No clue how it is working?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this gives you a clue:
 
Vigneshwari Chandrasekaran
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah thank you so much!!
 
Zhenyi Luo
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also be careful when str = "bbaaaaaaaaa"

s.length will be 1
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic