I\'m trying to split strings by capital letters, for example when the string is \"SizeColorSize\", the aoutput array is: {\"Size\", \"Color\", \"Size\"}. This works normally
When the beginning of a String
matches the regex pattern in String.split()
, it results in an additional empty String
.
So the split
will return you ["", "Size", "Color", "Size"] instead of ["Size", "Color", "Size], thus the behavior.
The official Javadoc of Split said the following:
The array returned by this method contains each substring of the input sequence that is terminated by another subsequence that matches this pattern or is terminated by the end of the input sequence.
So I guess the leading empty String
qualifies as a "substring of the input sequence that is terminated by another subsequence that matches this pattern", as said in the doc.