diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index 7380031204a5a..f12351e8e0a24 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -48,8 +48,8 @@ public inline fun CharSequence.elementAtOrNull(index: Int): Char? { /** * Returns the first character matching the given [predicate], or `null` if no such character was found. - * - * @sample samples.collections.Collections.Elements.find + * + * @sample samples.text.Strings.find */ @kotlin.internal.InlineOnly public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? { diff --git a/libraries/stdlib/samples/test/samples/text/strings.kt b/libraries/stdlib/samples/test/samples/text/strings.kt index 99bced10f13e9..19a81978bb3f3 100644 --- a/libraries/stdlib/samples/test/samples/text/strings.kt +++ b/libraries/stdlib/samples/test/samples/text/strings.kt @@ -435,6 +435,18 @@ class Strings { assertPrints(matchDetails(inputString, toFind, 10), "Searching for 'ever' in 'Never ever give up' starting at position 10: Not found") } + @Sample + fun find() { + val text = "k1o2t3l4i5n6" + + val firstNumberInText = text.find { it.isDigit() } + val firstUpperCaseInText = text.find { it.isUpperCase() } + + + assertPrints(firstNumberInText, "1") + assertPrints(firstUpperCaseInText, null) + } + @Sample fun last() { val string = "Kotlin 1.4.0" diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 19834b61315b5..05e8e99b7ebaf 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -607,7 +607,12 @@ object Elements : TemplateGroupBase() { } builder { inline(Inline.Only) doc { "Returns the first ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." } - sample("samples.collections.Collections.Elements.find") + specialFor(CharSequences) { + sample("samples.text.Strings.find") + } + specialFor(ArraysOfUnsigned) { + sample("samples.collections.Collections.Elements.find") + } returns("T?") body { "return firstOrNull(predicate)"} }