commonMain.extensions.String.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-stdlib-macosx64 Show documentation
Show all versions of fluid-stdlib-macosx64 Show documentation
Potentially useful Kotlin standard library additions
package io.fluidsonic.stdlib
fun String.truncatedTo(maximumLength: Int, truncationSuffix: String = ""): String {
require(maximumLength >= 0) { "maximumLength must be >= 0" }
if (length <= maximumLength) {
return this
}
if (maximumLength <= truncationSuffix.length) {
return truncationSuffix.take(truncationSuffix.length)
}
return take(maximumLength - truncationSuffix.length) + truncationSuffix
}