From a90b72ffd873134e582abcbdb6718ffa5a282010 Mon Sep 17 00:00:00 2001 From: Gen Date: Mon, 6 Aug 2018 23:38:19 +0900 Subject: [PATCH] KT-20357 Add samples for padStart() and padEnd() --- .../samples/test/samples/text/strings.kt | 19 +++++++++++++++++-- libraries/stdlib/src/kotlin/text/Strings.kt | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/samples/test/samples/text/strings.kt b/libraries/stdlib/samples/test/samples/text/strings.kt index 77d9f708d3d..72dbfb5e9a6 100644 --- a/libraries/stdlib/samples/test/samples/text/strings.kt +++ b/libraries/stdlib/samples/test/samples/text/strings.kt @@ -1,8 +1,6 @@ package samples.text import samples.* -import kotlin.test.* -import java.util.* class Strings { @@ -104,4 +102,21 @@ class Strings { assertPrints("Iced frappé!".toUpperCase(), "ICED FRAPPÉ!") } + @Sample + fun padStart() { + val padWithSpace = "a".padStart(3) + assertPrints("'$padWithSpace'", "' a'") + + val padWithChar = "a".padStart(3, '#') + assertPrints("'$padWithChar'", "'##a'") + } + + @Sample + fun padEnd() { + val padWithSpace = "a".padEnd(3) + assertPrints("'$padWithSpace'", "'a '") + + val padWithChar = "a".padEnd(3, '#') + assertPrints("'$padWithChar'", "'a##'") + } } diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index b81b8f7a68a..fd3d76c8212 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -153,6 +153,7 @@ public inline fun String.trimEnd(): String = (this as CharSequence).trimEnd().to * @param padChar the character to pad string with, if it has length less than the [length] specified. Space is used by default. * @return Returns a char sequence of length at least [length] consisting of `this` char sequence prepended with [padChar] as many times * as are necessary to reach that length. + * @sample samples.text.Strings.padStart */ public fun CharSequence.padStart(length: Int, padChar: Char = ' '): CharSequence { if (length < 0) @@ -174,6 +175,7 @@ public fun CharSequence.padStart(length: Int, padChar: Char = ' '): CharSequence * @param padChar the character to pad string with, if it has length less than the [length] specified. Space is used by default. * @return Returns a string of length at least [length] consisting of `this` string prepended with [padChar] as many times * as are necessary to reach that length. + * @sample samples.text.Strings.padStart */ public fun String.padStart(length: Int, padChar: Char = ' '): String = (this as CharSequence).padStart(length, padChar).toString() @@ -186,6 +188,7 @@ public fun String.padStart(length: Int, padChar: Char = ' '): String = * @param padChar the character to pad string with, if it has length less than the [length] specified. Space is used by default. * @return Returns a char sequence of length at least [length] consisting of `this` char sequence appended with [padChar] as many times * as are necessary to reach that length. + * @sample samples.text.Strings.padEnd */ public fun CharSequence.padEnd(length: Int, padChar: Char = ' '): CharSequence { if (length < 0) @@ -207,6 +210,7 @@ public fun CharSequence.padEnd(length: Int, padChar: Char = ' '): CharSequence { * @param padChar the character to pad string with, if it has length less than the [length] specified. Space is used by default. * @return Returns a string of length at least [length] consisting of `this` string appended with [padChar] as many times * as are necessary to reach that length. + * @sample samples.text.Strings.padEnd */ public fun String.padEnd(length: Int, padChar: Char = ' '): String = (this as CharSequence).padEnd(length, padChar).toString()