diff --git a/js/js.libraries/src/core/string.kt b/js/js.libraries/src/core/string.kt index ac110beeb00..e9fc480dc1a 100644 --- a/js/js.libraries/src/core/string.kt +++ b/js/js.libraries/src/core/string.kt @@ -18,23 +18,14 @@ internal fun String.nativeStartsWith(s: String, position: Int): Boolean = noImpl native("endsWith") internal fun String.nativeEndsWith(s: String): Boolean = noImpl -deprecated("Use split(Regex) instead.") +deprecated("Use split(Regex) instead.", ReplaceWith("split(regex.toRegex()).toTypedArray()")) library("splitString") public fun String.splitWithRegex(regex: String): Array = noImpl -deprecated("Use split(Regex) instead.") +deprecated("Use split(Regex) instead.", ReplaceWith("split(regex.toRegex(), limit = limit).toTypedArray()")) library("splitString") public fun String.splitWithRegex(regex: String, limit: Int): Array = noImpl -deprecated("Use split(Regex) instead.") -library("splitString") -public fun String.split(regex: String): Array = noImpl - -deprecated("Use split(Regex) instead.") -library("splitString") -public fun String.split(regex: String, limit: Int): Array = noImpl - - native public fun String.substring(beginIndex : Int) : String = noImpl native public fun String.substring(beginIndex : Int, endIndex : Int) : String = noImpl diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index e5e5195b32d..a4073631a4e 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -923,6 +923,10 @@ public fun String.splitToSequence(vararg delimiters: String, ignoreCase: Boolean * the beginning to the end of this string, and matches at each position the first element in [delimiters] * that is equal to a delimiter in this instance at that position. */ +public fun String.split(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List = + splitToSequence(*delimiters, ignoreCase = ignoreCase, limit = limit).toList() + +deprecated("Use split(delimiters) instead.", ReplaceWith("split(*delimiters, ignoreCase = ignoreCase, limit = limit)")) public fun String.splitBy(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List = splitToSequence(*delimiters, ignoreCase = ignoreCase, limit = limit).toList() diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index dfa54c17e63..6100d27dad7 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -128,17 +128,6 @@ public fun String.split(regex: Pattern, limit: Int = 0): List require(limit >= 0, { "Limit must be non-negative, but was $limit" } ) return regex.split(this, if (limit == 0) -1 else limit).asList() } -/** - * Splits this string around matches of the given regular expression. - */ -deprecated("Convert String argument to regex with toRegex or use splitBy instead for literal delimiters. Please note the change of return type.", ReplaceWith("split(regex.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()")) -public fun String.split(regex: String): Array = (this as java.lang.String).split(regex) - -/** - * Splits this string into at most [limit] chunks around matches of the given regular expression. - */ -deprecated("Convert String argument to regex with toRegex or use splitBy instead for literal delimiters. Please note the change of return type and the restriction of limit to be non-negative.", ReplaceWith("split(regex.toRegex(), limit.coerceAtLeast(0)).toTypedArray()")) -public fun String.split(regex: String, limit: Int): Array = (this as java.lang.String).split(regex, limit) /** * Returns a substring of this string starting with the specified index. diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 9e9c9da078c..d55ac51c03d 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -353,18 +353,18 @@ class StringTest { test fun split() { - assertEquals(listOf(""), "".splitBy(";")) + assertEquals(listOf(""), "".split(";")) assertEquals(listOf("test"), "test".split(*charArray()), "empty list of delimiters, none matched -> entire string returned") - assertEquals(listOf("test"), "test".splitBy(*array()), "empty list of delimiters, none matched -> entire string returned") + assertEquals(listOf("test"), "test".split(*array()), "empty list of delimiters, none matched -> entire string returned") assertEquals(listOf("abc", "def", "123;456"), "abc;def,123;456".split(';', ',', limit = 3)) - assertEquals(listOf("abc", "def", "123", "456"), "abc
def
123
456".splitBy("
", ignoreCase = true)) + assertEquals(listOf("abc", "def", "123", "456"), "abc
def
123
456".split("
", ignoreCase = true)) - assertEquals(listOf("abc", "def", "123", "456"), "abc=-def==123=456".splitBy("==", "=-", "=")) + assertEquals(listOf("abc", "def", "123", "456"), "abc=-def==123=456".split("==", "=-", "=")) - assertEquals(listOf("", "a", "b", "c", ""), "abc".splitBy("")) - assertEquals(listOf("", "a", "b", "b", "a", ""), "abba".splitBy("", "a")) - assertEquals(listOf("", "", "b", "b", "", ""), "abba".splitBy("a", "")) + assertEquals(listOf("", "a", "b", "c", ""), "abc".split("")) + assertEquals(listOf("", "a", "b", "b", "a", ""), "abba".split("", "a")) + assertEquals(listOf("", "", "b", "b", "", ""), "abba".split("a", "")) } test fun splitToLines() {