commonMain.io.github.lyxnx.util.String.kt Maven / Gradle / Ivy
@file:JvmName("Strings")
package io.github.lyxnx.util
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
/**
* Returns a string containing this char sequence repeated [n] times
*
* If [n] is zero or negative, an empty string is returned instead
*
* @see CharSequence.repeat
*/
@JvmSynthetic // jvm already has "".repeat(n) since JDK 11
public operator fun CharSequence.times(n: Int): String = repeat(n.coerceAtLeast(0))
/**
* Tests whether this char sequence contains, ignoring case, the [other] sequence
*
* @see CharSequence.contains
*/
public infix fun CharSequence.containsIgnoreCase(other: CharSequence): Boolean = contains(other, ignoreCase = true)
/**
* Returns this sequence if it does not contain blank characters, otherwise null
*
* @see CharSequence.isBlank
*/
public fun C.nullIfBlank(): C? = ifBlank { null }
/**
* Returns this sequence if it does not contain any character, otherwise null
*
* @see CharSequence.isEmpty
*/
public fun C.nullIfEmpty(): C? = ifEmpty { null }
/**
* Returns a sub sequence of this sequence, starting at [from] (inclusive) and ending at [to] (exclusive)
*/
@JvmSynthetic // java can use regular substring methods
public operator fun CharSequence.get(from: Int, to: Int): CharSequence = subSequence(from, to)
/**
* Returns a substring of this string, starting at [from] (inclusive) and ending at [to] (exclusive)
*/
@JvmSynthetic // java can use regular substring methods
public operator fun String.get(from: Int, to: Int): String = substring(from, to)
/**
* Returns this string if it is not blank, otherwise null
*
* @see CharSequence.isNotBlank
*/
public fun C.takeIfNotBlank(): C? = takeIf { it.isNotBlank() }
/**
* Returns this string if it is not empty, otherwise null
*
* @see CharSequence.isNotEmpty
*/
public fun C.takeIfNotEmpty(): C? = takeIf { it.isNotEmpty() }