title: Difference between Kotlin and Java String split with Regex
body: <p>If we have a <code>val txt: kotlin.String = "1;2;3;"</code> and like to split it into an array of numbers, we can try the following:</p>
  
  <pre><code>val numbers = string.split(";".toRegex())
  //gives: [1, 2, 3, ]
  </code></pre>
  
  <p>The trailing empty <code>String</code> is included in the result of <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/split.html" rel="noreferrer"><code>CharSequence.split</code></a>.</p>
  
  <p>On the other hand, if we look at Java <code>String</code>s, the result is different:</p>
  
  <pre><code>val numbers2 = (string as java.lang.String).split(";")
  //gives: [1, 2, 3]
  </code></pre>
  
  <p>This time, using <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)" rel="noreferrer"><code>java.lang.String.split</code></a>, the result does not include the trailing empty <code>String</code>. This behaviour actually is intended given the corresponding JavaDoc:</p>
  
  <blockquote>
    <p>This method works as if by invoking the two-argument split method with the given expression and a <em>limit argument of zero</em>. <strong>Trailing empty strings are therefore not included in the resulting array</strong>. </p>
  </blockquote>
  
  <p>In Kotlin's version though, <code>0</code> also is the default <code>limit</code> argument as documented <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/split.html" rel="noreferrer">here</a>, yet internally Kotlin maps that <code>0</code> on a negative value <code>-1</code> when <code>java.util.regex.Pattern::split</code> is <a href="https://github.com/JetBrains/kotlin/blob/1.2.20/libraries/stdlib/src/kotlin/text/regex/Regex.kt#L192" rel="noreferrer">called</a>:</p>
  
  <pre><code>nativePattern.split(input, if (limit == 0) -1 else limit).asList()
  </code></pre>
  
  <p>It seems to be working as intended but I'm wondering why the language seems to be restricting the Java API since a limit of <code>0</code> is not provided anymore.</p>