Introduce StringBuilder.clear() extension
#KT-18910 Fixed
This commit is contained in:
@@ -129,6 +129,15 @@ public expect fun String.decapitalize(): String
|
||||
public expect fun CharSequence.repeat(n: Int): String
|
||||
|
||||
|
||||
/**
|
||||
* Clears the content of this string builder making it empty.
|
||||
*
|
||||
* @sample samples.text.Strings.clearStringBuilder
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public expect fun StringBuilder.clear(): StringBuilder
|
||||
|
||||
|
||||
/**
|
||||
* Returns a new string with all occurrences of [oldChar] replaced with [newChar].
|
||||
*/
|
||||
|
||||
@@ -53,5 +53,27 @@ public actual class StringBuilder(content: String = "") : Appendable, CharSequen
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the content of this string builder making it empty.
|
||||
*
|
||||
* @sample samples.text.Strings.clearStringBuilder
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun clear(): StringBuilder {
|
||||
string = ""
|
||||
return this
|
||||
}
|
||||
|
||||
override fun toString(): String = string
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Clears the content of this string builder making it empty.
|
||||
*
|
||||
* @sample samples.text.Strings.clearStringBuilder
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
public actual fun StringBuilder.clear(): StringBuilder = this.clear()
|
||||
@@ -14,6 +14,13 @@ package kotlin.text
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun StringBuilder.set(index: Int, value: Char): Unit = this.setCharAt(index, value)
|
||||
|
||||
/**
|
||||
* Clears the content of this string builder making it empty.
|
||||
*
|
||||
* @sample samples.text.Strings.clearStringBuilder
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual fun StringBuilder.clear(): StringBuilder = apply { setLength(0) }
|
||||
|
||||
private object SystemProperties {
|
||||
/** Line separator for current system. */
|
||||
|
||||
@@ -127,4 +127,14 @@ class Strings {
|
||||
val noPadding = "abcde".padEnd(3)
|
||||
assertPrints("'$noPadding'", "'abcde'")
|
||||
}
|
||||
@Sample
|
||||
fun clearStringBuilder() {
|
||||
val builder = StringBuilder()
|
||||
builder.append("content").append(1)
|
||||
assertPrints(builder, "content1")
|
||||
|
||||
builder.clear()
|
||||
assertPrints(builder, "")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -66,4 +66,13 @@ class StringBuilderTest {
|
||||
assertEquals("content", sb.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun clear() {
|
||||
val sb = StringBuilder()
|
||||
sb.append("test")
|
||||
val s = sb.toString()
|
||||
sb.clear()
|
||||
assertTrue(sb.isEmpty())
|
||||
assertEquals("test", s)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user