Java String's split method ignores empty substrings

后端 未结 2 1079
借酒劲吻你
借酒劲吻你 2020-12-10 05:26

It occured to me today the behavior of java String.split() is very strange.

Actually I want to split a string \"aa,bb,cc,dd,,,ee\" to array

2条回答
  •  自闭症患者
    2020-12-10 05:41

    Use String.split(String regex, int limit) with negative limit (e.g. -1).

    "aa,bb,cc,dd,,,,".split(",", -1)
    

    When String.split(String regex) is called, it is called with limit = 0, which will remove all trailing empty strings in the array (in most cases, see below).

    The actual behavior of String.split(String regex) is quite confusing:

    • Splitting an empty string will result in an array of length 1. Empty string split will always result in length 1 array containing the empty string.
    • Splitting ";" or ";;;" with regex being ";" will result in an empty array. Non-empty string split will result in all trailing empty strings in the array removed.

    The behavior above can be observed from at least Java 5 to Java 8.

    There was an attempt to change the behavior to return an empty array when splitting an empty string in JDK-6559590. However, it was soon reverted in JDK-8028321 when it causes regression in various places. The change never makes it into the initial Java 8 release.

提交回复
热议问题