Skip to content

扩展函数

在 Kotlin 中,扩展函数允许你向现有的类添加新的函数,而无需修改其源代码。这为编写更具表达力和可读性的代码提供了便利。以下是一个示例:

kotlin
fun String.removeWhitespace(): String {
    return this.replace(" ", "")
}

val text = "Hello World"
val result = text.removeWhitespace()
// result: "HelloWorld"

Released under the MIT License.