• Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting empty strings vs. splitting strings that consist of only separators  RSS feed

 
Kev Kit
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you split an empty string, you get an array of 1 element
After you split a string that have only separators(for example, only special characters which the split(\\W) will remove), you get an array of 0 elements?

They both lead to empty tokens so surely they have the same length arrays?
But why not?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the documentation explicitly mentions that trailing empty strings at the end will be discarded if the limit is 0. That means that a string that consists only of separators will have all its empty string between the separators discarded. The documentation also explicitly states that if there is no match, the result array will contain exactly the input string as its only element. That means that applying a pattern with a positive width to the empty string will always yield the empty string. I bet that if you use a zero width pattern, the result array will also be empty.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!