Extra comma in HashSet

后端 未结 1 1249
孤街浪徒
孤街浪徒 2020-12-22 04:17

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

相关标签:
1条回答
  • 2020-12-22 05:16

    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.

    0 讨论(0)
提交回复
热议问题