diff --git a/js/js.libraries/src/core/string.kt b/js/js.libraries/src/core/string.kt index dae143b03b6..8f889d08437 100644 --- a/js/js.libraries/src/core/string.kt +++ b/js/js.libraries/src/core/string.kt @@ -11,10 +11,19 @@ native("lastIndexOf") public fun String.nativeLastIndexOf(str : String, fromIndex : Int) : Int = noImpl library("splitString") -public fun String.splitByRegex(regex: String): Array = noImpl +public fun String.splitWithRegex(regex: String): Array = noImpl library("splitString") -public fun String.splitByRegex(regex: String, limit: Int): Array = noImpl +public fun String.splitWithRegex(regex: String, limit: Int): Array = noImpl + +deprecated("Use splitWithRegex (temporary)") +library("splitString") +public fun String.split(regex: String): Array = noImpl + +deprecated("Use splitWithRegex (temporary)") +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 ad13569ea87..d7b5977a634 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -760,7 +760,8 @@ 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 = +deprecated("Temporary name 'splitBy' shall be replaced soon with 'split'.") +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 7970f5af5f2..17d4f9349f3 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -95,6 +95,12 @@ public fun String.format(locale: Locale, vararg args : Any?) : String = java.lan */ public fun String.split(regex: Pattern, limit: Int = 0): List = regex.split(this, limit).asList() +/** + * Splits this string around matches of the given regular expression. + */ +deprecated("Convert an argument to regex with toRegex or use splitBy instead.") +public fun String.split(regex: String): Array = split(regex.toRegex()).copyToArray() + /** * Returns a substring of this string starting with the specified index. diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 6c0b04cc79f..bd0e9a7bbd3 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -479,7 +479,7 @@ class CollectionTest { } test fun decomposeSplit() { - val (key, value) = "key = value".split("=").map { it.trim() } + val (key, value) = "key = value".splitBy("=").map { it.trim() } assertEquals(key, "key") assertEquals(value, "value") } diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 00ebce6d464..394ce0686f4 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -292,14 +292,14 @@ class StringTest { test fun split() { - assertEquals(listOf(""), "".split(";")) + assertEquals(listOf(""), "".splitBy(";")) assertEquals(listOf("test"), "test".split(*charArray()), "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("test"), "test".splitBy(*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".split("
", ignoreCase = true)) + assertEquals(listOf("abc", "def", "123", "456"), "abc
def
123
456".splitBy("
", ignoreCase = true)) - assertEquals(listOf("abc", "def", "123", "456"), "abc=-def==123=456".split("==", "=-", "=")) + assertEquals(listOf("abc", "def", "123", "456"), "abc=-def==123=456".splitBy("==", "=-", "=")) } test fun splitToLines() {