String.split(Pattern, limit) now treats limit differently: 0 means no limit is specified, but trailing empty elements are not stripped; -1 and other negative values are not allowed.

This commit is contained in:
Ilya Gorbunov
2015-04-06 21:00:34 +03:00
parent b1255cf95b
commit fb5a3a771c
2 changed files with 18 additions and 1 deletions
@@ -120,8 +120,14 @@ public fun String.format(locale: Locale, vararg args : Any?) : String = java.lan
/**
* Splits this string around matches of the given regular expression.
* @param limit The maximum number of substrings to return. Zero by default means no limit is set.
*/
public fun String.split(regex: Pattern, limit: Int = 0): List<String> = regex.split(this, limit).asList()
public fun String.split(regex: Pattern, limit: Int = 0): List<String>
{
require(limit >= 0, { "Limit must be non-negative, but was $limit" } )
return regex.split(this, if (limit == 0) -1 else limit).asList()
}
/**
* Returns a substring of this string starting with the specified index.