Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualpatrrr

Posted 02 April 2013 - 03:22 AM

I just recently found out that Java's String split method works in (at least for me) unexpected ways:

 

"hello".split(",").length  // -> 1
",".split(",").length      // -> 0
",,".split(",").length     // -> 0
" ,,".split(",").length    // -> 1
"".split(",").length       // -> 1

 

Is it just me that thinks this is weird?

  • Did they forget about empty strings?
  • Isn't this a possible source of bugs -- I'm thinking CSV, etc.
  • Isn't "split" breaking SRP, meaning, it doesn't just split, it also checks if the result is non-empty

Is there some possible explanation why they decided to do this? Mostly out of curiousity.


#1patrrr

Posted 02 April 2013 - 03:21 AM

I just recently found out that Java's String split method works in (at least for me) unexpected ways:

 

"hello".split(",").length  // -> 1
",".split(",").length      // -> 0
",,".split(",").length     // -> 0
" ,,".split(",").length    // -> 1
"".split(",").length       // -> 0

 

Is it just me that thinks this is weird?

  • Did they forget about empty strings?
  • Isn't this a possible source of bugs -- I'm thinking CSV, etc.
  • Isn't "split" breaking SRP, meaning, it doesn't just split, it also checks if the result is non-empty

Is there some possible explanation why they decided to do this? Mostly out of curiousity.


PARTNERS