String

The String class represents character strings. All string literals in Kotlin programs, such as "abc", are implemented as instances of this class.

Since Kotlin

1.0

Constructors

Link copied to clipboard
fun String()

Types

Link copied to clipboard
object Companion
Since Kotlin 1.0

Functions

Link copied to clipboard
open operator override fun compareTo(other: String): Int

Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.

Since Kotlin 1.0
Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Since Kotlin 1.0
Link copied to clipboard
open operator override fun get(index: Int): Char

Returns the character of this string at the specified index.

Since Kotlin 1.0
Link copied to clipboard
operator fun plus(other: Any?): String

Returns a string obtained by concatenating this string with the string representation of the given other object.

Since Kotlin 1.0
Link copied to clipboard
open override fun subSequence(startIndex: Int, endIndex: Int): CharSequence

Returns a new character sequence that is a subsequence of this character sequence, starting at the specified startIndex and ending right before the specified endIndex.

Since Kotlin 1.0
Link copied to clipboard
open override fun toString(): String
Since Kotlin 1.0

Properties

Link copied to clipboard
open override val length: Int

Returns the length of this character sequence.

Since Kotlin 1.0

Extensions

Link copied to clipboard
inline fun String.byteInputStream(charset: Charset = Charsets.UTF_8): ByteArrayInputStream

Creates a new byte input stream for the string.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.capitalize(): String

Returns a copy of this string having its first letter titlecased using the rules of the default locale, or the original string if it's empty or already starts with a title case letter.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.codePointAt(index: Int): Int

Returns the character (Unicode code point) at the specified index.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.codePointBefore(index: Int): Int

Returns the character (Unicode code point) before the specified index.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int

Returns the number of Unicode code points in the specified text range of this String.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.compareTo(other: String, ignoreCase: Boolean = false): Int

Compares two strings lexicographically, optionally ignoring case differences.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.contentEquals(charSequence: CharSequence): Boolean

Returns true if this string is equal to the contents of the specified CharSequence, false otherwise.

Since Kotlin 1.0
inline fun String.contentEquals(stringBuilder: StringBuffer): Boolean

Returns true if this string is equal to the contents of the specified StringBuffer, false otherwise.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.decapitalize(): String

Returns a copy of this string having its first letter lowercased using the rules of the default locale, or the original string if it's empty or already starts with a lower case letter.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun CharSequence.elementAt(index: Int): Char

Returns a character at the given index or throws an IndexOutOfBoundsException if the index is out of bounds of this char sequence.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean

Returns true if this string ends with the specified suffix.

Since Kotlin 1.0
Link copied to clipboard
actual fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean

Returns true if this string is equal to other, optionally ignoring character case.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.format(vararg args: Any?): String

Uses this string as a format string and returns a string obtained by substituting the specified arguments, using the default locale.

Since Kotlin 1.0
inline fun String.format(locale: Locale, vararg args: Any?): String

Uses this string as a format string and returns a string obtained by substituting the specified arguments, using the specified locale.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.intern(): String

Returns a canonical representation for this string object.

Since Kotlin 1.0
Link copied to clipboard

Returns true if this string is empty or consists solely of whitespace characters.

Since Kotlin 1.0
Link copied to clipboard
Since Kotlin 1.0
Link copied to clipboard
inline fun <R : Comparable<R>> CharSequence.maxBy(selector: (Char) -> R): Char?
Since Kotlin 1.0
Link copied to clipboard
fun CharSequence.maxWith(comparator: Comparator<in Char>): Char?
Since Kotlin 1.0
Link copied to clipboard
Since Kotlin 1.0
Link copied to clipboard
inline fun <R : Comparable<R>> CharSequence.minBy(selector: (Char) -> R): Char?
Since Kotlin 1.0
Link copied to clipboard
fun CharSequence.minWith(comparator: Comparator<in Char>): Char?
Since Kotlin 1.0
Link copied to clipboard
inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int

Returns the index within this string that is offset from the given index by codePointOffset code points.

Since Kotlin 1.0
Link copied to clipboard
operator fun String?.plus(other: Any?): String

Concatenates this string with the string representation of the given other object. If either the receiver or the other object are null, they are represented as the string "null".

Since Kotlin 1.0
Link copied to clipboard
inline fun String.reader(): StringReader

Creates a new reader for the string.

Since Kotlin 1.0
Link copied to clipboard
fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean

Returns true if the specified range in this string is equal to the specified range in another string.

Since Kotlin 1.0
actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean

Returns true if the specified range in this char sequence is equal to the specified range in another char sequence.

Since Kotlin 1.0
Link copied to clipboard
actual fun CharSequence.repeat(n: Int): String

Returns a string containing this char sequence repeated n times.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String

Returns a new string with all occurrences of oldChar replaced with newChar.

Since Kotlin 1.0
actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String

Returns a new string obtained by replacing all occurrences of the oldValue substring in this string with the specified newValue string.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String

Returns a new string with the first occurrence of oldChar replaced with newChar.

Since Kotlin 1.0
actual fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String

Returns a new string obtained by replacing the first occurrence of the oldValue substring in this string with the specified newValue string.

Since Kotlin 1.0
Link copied to clipboard
fun CharSequence.split(regex: Pattern, limit: Int = 0): List<String>

Splits this char sequence around matches of the given regular expression.

Since Kotlin 1.0
Link copied to clipboard
actual fun String.startsWith(prefix: String, ignoreCase: Boolean = false): Boolean

Returns true if this string starts with the specified prefix.

Since Kotlin 1.0
actual fun String.startsWith(prefix: String, startIndex: Int, ignoreCase: Boolean = false): Boolean

Returns true if a substring of this string starting at the specified offset startIndex starts with the specified prefix.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.substring(startIndex: Int): String

Returns a substring of this string that starts at the specified startIndex and continues to the end of the string.

Since Kotlin 1.0
actual inline fun String.substring(startIndex: Int, endIndex: Int): String

Returns the substring of this string starting at the startIndex and ending right before the endIndex.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toBoolean(): Boolean

Returns true if the content of this string is equal to the word "true", ignoring case, and false otherwise.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toByte(): Byte

Parses the string as a signed Byte number and returns the result.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.toByteArray(charset: Charset = Charsets.UTF_8): ByteArray

Encodes the contents of this string using the specified character set and returns the resulting byte array.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toCharArray(): CharArray

Returns a CharArray containing characters of this string.

Since Kotlin 1.0
inline fun String.toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = length): CharArray

Copies characters from this string into the destination character array and returns that array.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toDouble(): Double

Parses the string as a Double number and returns the result.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toFloat(): Float

Parses the string as a Float number and returns the result.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toInt(): Int

Parses the string as an Int number and returns the result.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toLong(): Long

Parses the string as a Long number and returns the result.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toLowerCase(): String

Returns a copy of this string converted to lower case using the rules of the default locale.

Since Kotlin 1.0
inline fun String.toLowerCase(locale: Locale): String

Returns a copy of this string converted to lower case using the rules of the specified locale.

Since Kotlin 1.0
Link copied to clipboard
inline fun String.toPattern(flags: Int = 0): Pattern

Converts the string into a regular expression Pattern optionally with the specified flags from Pattern or'd together so that strings can be split or matched on.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toShort(): Short

Parses the string as a Short number and returns the result.

Since Kotlin 1.0
Link copied to clipboard

Returns a new SortedSet of all characters.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun String.toUpperCase(): String

Returns a copy of this string converted to upper case using the rules of the default locale.

Since Kotlin 1.0
inline fun String.toUpperCase(locale: Locale): String

Returns a copy of this string converted to upper case using the rules of the specified locale.

Since Kotlin 1.0