W3cubDocs

/Kotlin

String

class String : Comparable<String>, CharSequence

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

Constructors

<init>

String()

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

Properties

length

val length: Int

Returns the length of this character sequence.

Functions

compareTo

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.

get

fun get(index: Int): Char

Returns the character at the specified index in this character sequence.

plus

operator fun plus(other: Any?): String

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

subSequence

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.

Extension Properties

indices

val CharSequence.indices: IntRange

Returns the range of valid character indices for this char sequence.

lastIndex

val CharSequence.lastIndex: Int

Returns the index of the last character in the char sequence or -1 if it is empty.

size

val CharSequence.size: Int

Extension Functions

all

fun CharSequence.all(predicate: (Char) -> Boolean): Boolean

Returns true if all characters match the given predicate.

any

fun CharSequence.any(): Boolean

Returns true if char sequence has at least one character.

fun CharSequence.any(predicate: (Char) -> Boolean): Boolean

Returns true if at least one character matches the given predicate.

asIterable

fun CharSequence.asIterable(): Iterable<Char>

Creates an Iterable instance that wraps the original char sequence returning its characters when being iterated.

asSequence

fun CharSequence.asSequence(): Sequence<Char>

Creates a Sequence instance that wraps the original char sequence returning its characters when being iterated.

associate

fun <K, V> CharSequence.associate(
    transform: (Char) -> Pair<K, V>
): Map<K, V>

Returns a Map containing key-value pairs provided by transform function applied to characters of the given char sequence.

associateBy

fun <K> CharSequence.associateBy(
    keySelector: (Char) -> K
): Map<K, Char>

Returns a Map containing the characters from the given char sequence indexed by the key returned from keySelector function applied to each character.

fun <K, V> CharSequence.associateBy(
    keySelector: (Char) -> K, 
    valueTransform: (Char) -> V
): Map<K, V>

Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to characters of the given char sequence.

associateByTo

fun <K, M : MutableMap<in K, in Char>> CharSequence.associateByTo(
    destination: M, 
    keySelector: (Char) -> K
): M

Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function applied to each character of the given char sequence and value is the character itself.

fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateByTo(
    destination: M, 
    keySelector: (Char) -> K, 
    valueTransform: (Char) -> V
): M

Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function and and value is provided by the valueTransform function applied to characters of the given char sequence.

associateTo

fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateTo(
    destination: M, 
    transform: (Char) -> Pair<K, V>
): M

Populates and returns the destination mutable map with key-value pairs provided by transform function applied to each character of the given char sequence.

byteInputStream

fun String.byteInputStream(
    charset: Charset = Charsets.UTF_8
): ByteArrayInputStream

Creates a new byte input stream for the string.

capitalize

fun String.capitalize(): String

Returns a copy of this string having its first letter uppercased, or the original string, if it's empty or already starts with an upper case letter.

chunked

fun CharSequence.chunked(size: Int): List<String>

Splits this char sequence into a list of strings each not exceeding the given size.

fun <R> CharSequence.chunked(
    size: Int, 
    transform: (CharSequence) -> R
): List<R>

Splits this char sequence into several char sequences each not exceeding the given size and applies the given transform function to an each.

chunkedSequence

fun CharSequence.chunkedSequence(size: Int): Sequence<String>

Splits this char sequence into a sequence of strings each not exceeding the given size.

fun <R> CharSequence.chunkedSequence(
    size: Int, 
    transform: (CharSequence) -> R
): Sequence<R>

Splits this char sequence into several char sequences each not exceeding the given size and applies the given transform function to an each.

codePointAt

fun String.codePointAt(index: Int): Int

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

codePointBefore

fun String.codePointBefore(index: Int): Int

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

codePointCount

fun String.codePointCount(
    beginIndex: Int, 
    endIndex: Int
): Int

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

commonPrefixWith

fun CharSequence.commonPrefixWith(
    other: CharSequence, 
    ignoreCase: Boolean = false
): String

Returns the longest string prefix such that this char sequence and other char sequence both start with this prefix, taking care not to split surrogate pairs. If this and other have no common prefix, returns the empty string.

commonSuffixWith

fun CharSequence.commonSuffixWith(
    other: CharSequence, 
    ignoreCase: Boolean = false
): String

Returns the longest string suffix such that this char sequence and other char sequence both end with this suffix, taking care not to split surrogate pairs. If this and other have no common suffix, returns the empty string.

compareTo

fun String.compareTo(
    other: String, 
    ignoreCase: Boolean = false
): Int

Compares two strings lexicographically, optionally ignoring case differences.

concat

fun String.concat(str: String): String

contains

operator fun CharSequence.contains(
    other: CharSequence, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this char sequence contains the specified other sequence of characters as a substring.

operator fun CharSequence.contains(
    char: Char, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this char sequence contains the specified character char.

operator fun CharSequence.contains(regex: Regex): Boolean

Returns true if this char sequence contains at least one match of the specified regular expression regex.

contentEquals

fun String.contentEquals(charSequence: CharSequence): Boolean

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

fun String.contentEquals(
    stringBuilder: StringBuffer
): Boolean

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

count

fun CharSequence.count(): Int

Returns the length of this char sequence.

fun CharSequence.count(predicate: (Char) -> Boolean): Int

Returns the number of characters matching the given predicate.

decapitalize

fun String.decapitalize(): String

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

drop

fun String.drop(n: Int): String

Returns a string with the first n characters removed.

dropLast

fun String.dropLast(n: Int): String

Returns a string with the last n characters removed.

dropLastWhile

fun String.dropLastWhile(
    predicate: (Char) -> Boolean
): String

Returns a string containing all characters except last characters that satisfy the given predicate.

dropWhile

fun String.dropWhile(predicate: (Char) -> Boolean): String

Returns a string containing all characters except first characters that satisfy the given predicate.

elementAt

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.

elementAtOrElse

fun CharSequence.elementAtOrElse(
    index: Int, 
    defaultValue: (Int) -> Char
): Char

Returns a character at the given index or the result of calling the defaultValue function if the index is out of bounds of this char sequence.

elementAtOrNull

fun CharSequence.elementAtOrNull(index: Int): Char?

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

endsWith

fun String.endsWith(
    suffix: String, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this string ends with the specified suffix.

fun CharSequence.endsWith(
    char: Char, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this char sequence ends with the specified character.

fun CharSequence.endsWith(
    suffix: CharSequence, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this char sequence ends with the specified suffix.

equals

fun String?.equals(
    other: String?, 
    ignoreCase: Boolean = false
): Boolean

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

filter

fun String.filter(predicate: (Char) -> Boolean): String

Returns a string containing only those characters from the original string that match the given predicate.

filterIndexed

fun String.filterIndexed(
    predicate: (index: Int, Char) -> Boolean
): String

Returns a string containing only those characters from the original string that match the given predicate.

filterIndexedTo

fun <C> CharSequence.filterIndexedTo(
    destination: C, 
    predicate: (index: Int, Char) -> Boolean
): C

Appends all characters matching the given predicate to the given destination.

filterNot

fun String.filterNot(predicate: (Char) -> Boolean): String

Returns a string containing only those characters from the original string that do not match the given predicate.

filterNotTo

fun <C> CharSequence.filterNotTo(
    destination: C, 
    predicate: (Char) -> Boolean
): C

Appends all characters not matching the given predicate to the given destination.

filterTo

fun <C> CharSequence.filterTo(
    destination: C, 
    predicate: (Char) -> Boolean
): C

Appends all characters matching the given predicate to the given destination.

find

fun CharSequence.find(predicate: (Char) -> Boolean): Char?

Returns the first character matching the given predicate, or null if no such character was found.

findAnyOf

fun CharSequence.findAnyOf(
    strings: Collection<String>, 
    startIndex: Int = 0, 
    ignoreCase: Boolean = false
): Pair<Int, String>?

Finds the first occurrence of any of the specified strings in this char sequence, starting from the specified startIndex and optionally ignoring the case.

findLast

fun CharSequence.findLast(
    predicate: (Char) -> Boolean
): Char?

Returns the last character matching the given predicate, or null if no such character was found.

findLastAnyOf

fun CharSequence.findLastAnyOf(
    strings: Collection<String>, 
    startIndex: Int = lastIndex, 
    ignoreCase: Boolean = false
): Pair<Int, String>?

Finds the last occurrence of any of the specified strings in this char sequence, starting from the specified startIndex and optionally ignoring the case.

first

fun CharSequence.first(): Char

Returns first character.

fun CharSequence.first(predicate: (Char) -> Boolean): Char

Returns the first character matching the given predicate.

firstOrNull

fun CharSequence.firstOrNull(): Char?

Returns the first character, or null if the char sequence is empty.

fun CharSequence.firstOrNull(
    predicate: (Char) -> Boolean
): Char?

Returns the first character matching the given predicate, or null if character was not found.

flatMap

fun <R> CharSequence.flatMap(
    transform: (Char) -> Iterable<R>
): List<R>

Returns a single list of all elements yielded from results of transform function being invoked on each character of original char sequence.

flatMapTo

fun <R, C : MutableCollection<in R>> CharSequence.flatMapTo(
    destination: C, 
    transform: (Char) -> Iterable<R>
): C

Appends all elements yielded from results of transform function being invoked on each character of original char sequence, to the given destination.

fold

fun <R> CharSequence.fold(
    initial: R, 
    operation: (acc: R, Char) -> R
): R

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each character.

foldIndexed

fun <R> CharSequence.foldIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Char) -> R
): R

Accumulates value starting with initial value and applying operation from left to right to current accumulator value and each character with its index in the original char sequence.

foldRight

fun <R> CharSequence.foldRight(
    initial: R, 
    operation: (Char, acc: R) -> R
): R

Accumulates value starting with initial value and applying operation from right to left to each character and current accumulator value.

foldRightIndexed

fun <R> CharSequence.foldRightIndexed(
    initial: R, 
    operation: (index: Int, Char, acc: R) -> R
): R

Accumulates value starting with initial value and applying operation from right to left to each character with its index in the original char sequence and current accumulator value.

forEach

fun CharSequence.forEach(action: (Char) -> Unit)

Performs the given action on each character.

forEachIndexed

fun CharSequence.forEachIndexed(
    action: (index: Int, Char) -> Unit)

Performs the given action on each character, providing sequential index with the character.

format

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.

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.

getOrElse

fun CharSequence.getOrElse(
    index: Int, 
    defaultValue: (Int) -> Char
): Char

Returns a character at the given index or the result of calling the defaultValue function if the index is out of bounds of this char sequence.

getOrNull

fun CharSequence.getOrNull(index: Int): Char?

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

groupBy

fun <K> CharSequence.groupBy(
    keySelector: (Char) -> K
): Map<K, List<Char>>

Groups characters of the original char sequence by the key returned by the given keySelector function applied to each character and returns a map where each group key is associated with a list of corresponding characters.

fun <K, V> CharSequence.groupBy(
    keySelector: (Char) -> K, 
    valueTransform: (Char) -> V
): Map<K, List<V>>

Groups values returned by the valueTransform function applied to each character of the original char sequence by the key returned by the given keySelector function applied to the character and returns a map where each group key is associated with a list of corresponding values.

groupByTo

fun <K, M : MutableMap<in K, MutableList<Char>>> CharSequence.groupByTo(
    destination: M, 
    keySelector: (Char) -> K
): M

Groups characters of the original char sequence by the key returned by the given keySelector function applied to each character and puts to the destination map each group key associated with a list of corresponding characters.

fun <K, V, M : MutableMap<in K, MutableList<V>>> CharSequence.groupByTo(
    destination: M, 
    keySelector: (Char) -> K, 
    valueTransform: (Char) -> V
): M

Groups values returned by the valueTransform function applied to each character of the original char sequence by the key returned by the given keySelector function applied to the character and puts to the destination map each group key associated with a list of corresponding values.

groupingBy

fun <K> CharSequence.groupingBy(
    keySelector: (Char) -> K
): Grouping<Char, K>

Creates a Grouping source from a char sequence to be used later with one of group-and-fold operations using the specified keySelector function to extract a key from each character.

hasSurrogatePairAt

fun CharSequence.hasSurrogatePairAt(index: Int): Boolean

Returns true if this CharSequence has Unicode surrogate pair at the specified index.

indexOf

fun CharSequence.indexOf(
    char: Char, 
    startIndex: Int = 0, 
    ignoreCase: Boolean = false
): Int

Returns the index within this string of the first occurrence of the specified character, starting from the specified startIndex.

fun CharSequence.indexOf(
    string: String, 
    startIndex: Int = 0, 
    ignoreCase: Boolean = false
): Int

Returns the index within this char sequence of the first occurrence of the specified string, starting from the specified startIndex.

indexOfAny

fun CharSequence.indexOfAny(
    chars: CharArray, 
    startIndex: Int = 0, 
    ignoreCase: Boolean = false
): Int

Finds the index of the first occurrence of any of the specified chars in this char sequence, starting from the specified startIndex and optionally ignoring the case.

fun CharSequence.indexOfAny(
    strings: Collection<String>, 
    startIndex: Int = 0, 
    ignoreCase: Boolean = false
): Int

Finds the index of the first occurrence of any of the specified strings in this char sequence, starting from the specified startIndex and optionally ignoring the case.

indexOfFirst

fun CharSequence.indexOfFirst(
    predicate: (Char) -> Boolean
): Int

Returns index of the first character matching the given predicate, or -1 if the char sequence does not contain such character.

indexOfLast

fun CharSequence.indexOfLast(
    predicate: (Char) -> Boolean
): Int

Returns index of the last character matching the given predicate, or -1 if the char sequence does not contain such character.

intern

fun String.intern(): String

Returns a canonical representation for this string object.

isBlank

fun CharSequence.isBlank(): Boolean

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

isEmpty

fun CharSequence.isEmpty(): Boolean

Returns true if this char sequence is empty (contains no characters).

isNotBlank

fun CharSequence.isNotBlank(): Boolean

Returns true if this char sequence is not empty and contains some characters except of whitespace characters.

isNotEmpty

fun CharSequence.isNotEmpty(): Boolean

Returns true if this char sequence is not empty.

isNullOrBlank

fun CharSequence?.isNullOrBlank(): Boolean

Returns true if this nullable char sequence is either null or empty or consists solely of whitespace characters.

isNullOrEmpty

fun CharSequence?.isNullOrEmpty(): Boolean

Returns true if this nullable char sequence is either null or empty.

iterator

operator fun CharSequence.iterator(): CharIterator

Iterator for characters of the given char sequence.

last

fun CharSequence.last(): Char

Returns the last character.

fun CharSequence.last(predicate: (Char) -> Boolean): Char

Returns the last character matching the given predicate.

lastIndexOf

fun CharSequence.lastIndexOf(
    char: Char, 
    startIndex: Int = lastIndex, 
    ignoreCase: Boolean = false
): Int

Returns the index within this char sequence of the last occurrence of the specified character, starting from the specified startIndex.

fun CharSequence.lastIndexOf(
    string: String, 
    startIndex: Int = lastIndex, 
    ignoreCase: Boolean = false
): Int

Returns the index within this char sequence of the last occurrence of the specified string, starting from the specified startIndex.

lastIndexOfAny

fun CharSequence.lastIndexOfAny(
    chars: CharArray, 
    startIndex: Int = lastIndex, 
    ignoreCase: Boolean = false
): Int

Finds the index of the last occurrence of any of the specified chars in this char sequence, starting from the specified startIndex and optionally ignoring the case.

fun CharSequence.lastIndexOfAny(
    strings: Collection<String>, 
    startIndex: Int = lastIndex, 
    ignoreCase: Boolean = false
): Int

Finds the index of the last occurrence of any of the specified strings in this char sequence, starting from the specified startIndex and optionally ignoring the case.

lastOrNull

fun CharSequence.lastOrNull(): Char?

Returns the last character, or null if the char sequence is empty.

fun CharSequence.lastOrNull(
    predicate: (Char) -> Boolean
): Char?

Returns the last character matching the given predicate, or null if no such character was found.

lineSequence

fun CharSequence.lineSequence(): Sequence<String>

Splits this char sequence to a sequence of lines delimited by any of the following character sequences: CRLF, LF or CR.

lines

fun CharSequence.lines(): List<String>
  • Splits this char sequence to a list of lines delimited by any of the following character sequences: CRLF, LF or CR.

map

fun <R> CharSequence.map(transform: (Char) -> R): List<R>

Returns a list containing the results of applying the given transform function to each character in the original char sequence.

mapIndexed

fun <R> CharSequence.mapIndexed(
    transform: (index: Int, Char) -> R
): List<R>

Returns a list containing the results of applying the given transform function to each character and its index in the original char sequence.

mapIndexedNotNull

fun <R : Any> CharSequence.mapIndexedNotNull(
    transform: (index: Int, Char) -> R?
): List<R>

Returns a list containing only the non-null results of applying the given transform function to each character and its index in the original char sequence.

mapIndexedNotNullTo

fun <R : Any, C : MutableCollection<in R>> CharSequence.mapIndexedNotNullTo(
    destination: C, 
    transform: (index: Int, Char) -> R?
): C

Applies the given transform function to each character and its index in the original char sequence and appends only the non-null results to the given destination.

mapIndexedTo

fun <R, C : MutableCollection<in R>> CharSequence.mapIndexedTo(
    destination: C, 
    transform: (index: Int, Char) -> R
): C

Applies the given transform function to each character and its index in the original char sequence and appends the results to the given destination.

mapNotNull

fun <R : Any> CharSequence.mapNotNull(
    transform: (Char) -> R?
): List<R>

Returns a list containing only the non-null results of applying the given transform function to each character in the original char sequence.

mapNotNullTo

fun <R : Any, C : MutableCollection<in R>> CharSequence.mapNotNullTo(
    destination: C, 
    transform: (Char) -> R?
): C

Applies the given transform function to each character in the original char sequence and appends only the non-null results to the given destination.

mapTo

fun <R, C : MutableCollection<in R>> CharSequence.mapTo(
    destination: C, 
    transform: (Char) -> R
): C

Applies the given transform function to each character of the original char sequence and appends the results to the given destination.

match

fun String.match(regex: String): Array<String>?

matches

infix fun CharSequence.matches(regex: Regex): Boolean

Returns true if this char sequence matches the given regular expression.

fun String.matches(regex: String): Boolean

max

fun CharSequence.max(): Char?

Returns the largest character or null if there are no characters.

maxBy

fun <R : Comparable<R>> CharSequence.maxBy(
    selector: (Char) -> R
): Char?

Returns the first character yielding the largest value of the given function or null if there are no characters.

maxWith

fun CharSequence.maxWith(
    comparator: Comparator<in Char>
): Char?
fun CharSequence.maxWith(
    comparator: Comparator<in Char>
): Char?

Returns the first character having the largest value according to the provided comparator or null if there are no characters.

min

fun CharSequence.min(): Char?

Returns the smallest character or null if there are no characters.

minBy

fun <R : Comparable<R>> CharSequence.minBy(
    selector: (Char) -> R
): Char?

Returns the first character yielding the smallest value of the given function or null if there are no characters.

minWith

fun CharSequence.minWith(
    comparator: Comparator<in Char>
): Char?
fun CharSequence.minWith(
    comparator: Comparator<in Char>
): Char?

Returns the first character having the smallest value according to the provided comparator or null if there are no characters.

none

fun CharSequence.none(): Boolean

Returns true if the char sequence has no characters.

fun CharSequence.none(predicate: (Char) -> Boolean): Boolean

Returns true if no characters match the given predicate.

offsetByCodePoints

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.

orEmpty

fun String?.orEmpty(): String

Returns the string if it is not null, or the empty string otherwise.

padEnd

fun String.padEnd(length: Int, padChar: Char = ' '): String

Pads the string to the specified length at the end with the specified character or space.

padStart

fun String.padStart(length: Int, padChar: Char = ' '): String

Pads the string to the specified length at the beginning with the specified character or space.

partition

fun String.partition(
    predicate: (Char) -> Boolean
): Pair<String, String>

Splits the original string into pair of strings, where first string contains characters for which predicate yielded true, while second string contains characters for which predicate yielded false.

plus

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".

prependIndent

fun String.prependIndent(indent: String = "    "): String

Prepends indent to every line of the original string.

reader

fun String.reader(): StringReader

Creates a new reader for the string.

reduce

fun CharSequence.reduce(
    operation: (acc: Char, Char) -> Char
): Char

Accumulates value starting with the first character and applying operation from left to right to current accumulator value and each character.

reduceIndexed

fun CharSequence.reduceIndexed(
    operation: (index: Int, acc: Char, Char) -> Char
): Char

Accumulates value starting with the first character and applying operation from left to right to current accumulator value and each character with its index in the original char sequence.

reduceRight

fun CharSequence.reduceRight(
    operation: (Char, acc: Char) -> Char
): Char

Accumulates value starting with last character and applying operation from right to left to each character and current accumulator value.

reduceRightIndexed

fun CharSequence.reduceRightIndexed(
    operation: (index: Int, Char, acc: Char) -> Char
): Char

Accumulates value starting with last character and applying operation from right to left to each character with its index in the original char sequence and current accumulator value.

regionMatches

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.

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.

removePrefix

fun String.removePrefix(prefix: CharSequence): String

If this string starts with the given prefix, returns a copy of this string with the prefix removed. Otherwise, returns this string.

removeRange

fun String.removeRange(
    startIndex: Int, 
    endIndex: Int
): String

Removes the part of a string at a given range.

fun String.removeRange(range: IntRange): String

Removes the part of a string at the given range.

removeSuffix

fun String.removeSuffix(suffix: CharSequence): String

If this string ends with the given suffix, returns a copy of this string with the suffix removed. Otherwise, returns this string.

removeSurrounding

fun String.removeSurrounding(
    prefix: CharSequence, 
    suffix: CharSequence
): String

Removes from a string both the given prefix and suffix if and only if it starts with the prefix and ends with the suffix. Otherwise returns this string unchanged.

fun String.removeSurrounding(delimiter: CharSequence): String

Removes the given delimiter string from both the start and the end of this string if and only if it starts with and ends with the delimiter. Otherwise returns this string unchanged.

repeat

fun CharSequence.repeat(n: Int): String

Returns a string containing this char sequence repeated n times.

replace

fun String.replace(
    oldChar: Char, 
    newChar: Char, 
    ignoreCase: Boolean = false
): String

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

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.

fun CharSequence.replace(
    regex: Regex, 
    replacement: String
): String

Returns a new string obtained by replacing each substring of this char sequence that matches the given regular expression with the given replacement.

fun CharSequence.replace(
    regex: Regex, 
    transform: (MatchResult) -> CharSequence
): String

Returns a new string obtained by replacing each substring of this char sequence that matches the given regular expression with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

replaceAfter

fun String.replaceAfter(
    delimiter: Char, 
    replacement: String, 
    missingDelimiterValue: String = this
): String
fun String.replaceAfter(
    delimiter: String, 
    replacement: String, 
    missingDelimiterValue: String = this
): String

Replace part of string after the first occurrence of given delimiter with the replacement string. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

replaceAfterLast

fun String.replaceAfterLast(
    delimiter: String, 
    replacement: String, 
    missingDelimiterValue: String = this
): String
fun String.replaceAfterLast(
    delimiter: Char, 
    replacement: String, 
    missingDelimiterValue: String = this
): String

Replace part of string after the last occurrence of given delimiter with the replacement string. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

replaceBefore

fun String.replaceBefore(
    delimiter: Char, 
    replacement: String, 
    missingDelimiterValue: String = this
): String
fun String.replaceBefore(
    delimiter: String, 
    replacement: String, 
    missingDelimiterValue: String = this
): String

Replace part of string before the first occurrence of given delimiter with the replacement string. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

replaceBeforeLast

fun String.replaceBeforeLast(
    delimiter: Char, 
    replacement: String, 
    missingDelimiterValue: String = this
): String
fun String.replaceBeforeLast(
    delimiter: String, 
    replacement: String, 
    missingDelimiterValue: String = this
): String

Replace part of string before the last occurrence of given delimiter with the replacement string. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

replaceFirst

fun String.replaceFirst(
    oldChar: Char, 
    newChar: Char, 
    ignoreCase: Boolean = false
): String

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

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.

fun CharSequence.replaceFirst(
    regex: Regex, 
    replacement: String
): String

Replaces the first occurrence of the given regular expression regex in this char sequence with specified replacement expression.

replaceIndent

fun String.replaceIndent(newIndent: String = ""): String

Detects a common minimal indent like it does trimIndent and replaces it with the specified newIndent.

replaceIndentByMargin

fun String.replaceIndentByMargin(
    newIndent: String = "", 
    marginPrefix: String = "|"
): String

Detects indent by marginPrefix as it does trimMargin and replace it with newIndent.

replaceRange

fun String.replaceRange(
    startIndex: Int, 
    endIndex: Int, 
    replacement: CharSequence
): String

Replaces the part of the string at the given range with the replacement char sequence.

fun String.replaceRange(
    range: IntRange, 
    replacement: CharSequence
): String

Replace the part of string at the given range with the replacement string.

reversed

fun String.reversed(): String

Returns a string with characters in reversed order.

single

fun CharSequence.single(): Char

Returns the single character, or throws an exception if the char sequence is empty or has more than one character.

fun CharSequence.single(predicate: (Char) -> Boolean): Char

Returns the single character matching the given predicate, or throws exception if there is no or more than one matching character.

singleOrNull

fun CharSequence.singleOrNull(): Char?

Returns single character, or null if the char sequence is empty or has more than one character.

fun CharSequence.singleOrNull(
    predicate: (Char) -> Boolean
): Char?

Returns the single character matching the given predicate, or null if character was not found or more than one character was found.

slice

fun String.slice(indices: IntRange): String

Returns a string containing characters of the original string at the specified range of indices.

fun String.slice(indices: Iterable<Int>): String

Returns a string containing characters of the original string at specified indices.

split

fun CharSequence.split(
    vararg delimiters: String, 
    ignoreCase: Boolean = false, 
    limit: Int = 0
): List<String>
fun CharSequence.split(
    vararg delimiters: Char, 
    ignoreCase: Boolean = false, 
    limit: Int = 0
): List<String>

Splits this char sequence to a list of strings around occurrences of the specified delimiters.

fun CharSequence.split(
    regex: Regex, 
    limit: Int = 0
): List<String>
fun CharSequence.split(
    regex: Pattern, 
    limit: Int = 0
): List<String>

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

splitToSequence

fun CharSequence.splitToSequence(
    vararg delimiters: String, 
    ignoreCase: Boolean = false, 
    limit: Int = 0
): Sequence<String>
fun CharSequence.splitToSequence(
    vararg delimiters: Char, 
    ignoreCase: Boolean = false, 
    limit: Int = 0
): Sequence<String>

Splits this char sequence to a sequence of strings around occurrences of the specified delimiters.

startsWith

fun String.startsWith(
    prefix: String, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this string starts with the specified prefix.

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.

fun CharSequence.startsWith(
    char: Char, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this char sequence starts with the specified character.

fun CharSequence.startsWith(
    prefix: CharSequence, 
    ignoreCase: Boolean = false
): Boolean

Returns true if this char sequence starts with the specified prefix.

fun CharSequence.startsWith(
    prefix: CharSequence, 
    startIndex: Int, 
    ignoreCase: Boolean = false
): Boolean

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

subSequence

fun String.subSequence(start: Int, end: Int): CharSequence

Returns a subsequence of this char sequence.

fun CharSequence.subSequence(range: IntRange): CharSequence

Returns a subsequence of this char sequence specified by the given range of indices.

substring

fun String.substring(range: IntRange): String

Returns a substring specified by the given range of indices.

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.

fun String.substring(startIndex: Int, endIndex: Int): String

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

substringAfter

fun String.substringAfter(
    delimiter: Char, 
    missingDelimiterValue: String = this
): String
fun String.substringAfter(
    delimiter: String, 
    missingDelimiterValue: String = this
): String

Returns a substring after the first occurrence of delimiter. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

substringAfterLast

fun String.substringAfterLast(
    delimiter: Char, 
    missingDelimiterValue: String = this
): String
fun String.substringAfterLast(
    delimiter: String, 
    missingDelimiterValue: String = this
): String

Returns a substring after the last occurrence of delimiter. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

substringBefore

fun String.substringBefore(
    delimiter: Char, 
    missingDelimiterValue: String = this
): String
fun String.substringBefore(
    delimiter: String, 
    missingDelimiterValue: String = this
): String

Returns a substring before the first occurrence of delimiter. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

substringBeforeLast

fun String.substringBeforeLast(
    delimiter: Char, 
    missingDelimiterValue: String = this
): String
fun String.substringBeforeLast(
    delimiter: String, 
    missingDelimiterValue: String = this
): String

Returns a substring before the last occurrence of delimiter. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

sumBy

fun CharSequence.sumBy(selector: (Char) -> Int): Int

Returns the sum of all values produced by selector function applied to each character in the char sequence.

sumByDouble

fun CharSequence.sumByDouble(
    selector: (Char) -> Double
): Double

Returns the sum of all values produced by selector function applied to each character in the char sequence.

take

fun String.take(n: Int): String

Returns a string containing the first n characters from this string, or the entire string if this string is shorter.

takeLast

fun String.takeLast(n: Int): String

Returns a string containing the last n characters from this string, or the entire string if this string is shorter.

takeLastWhile

fun String.takeLastWhile(
    predicate: (Char) -> Boolean
): String

Returns a string containing last characters that satisfy the given predicate.

takeWhile

fun String.takeWhile(predicate: (Char) -> Boolean): String

Returns a string containing the first characters that satisfy the given predicate.

toBigDecimal

fun String.toBigDecimal(): BigDecimal
fun String.toBigDecimal(mathContext: MathContext): BigDecimal

Parses the string as a java.math.BigDecimal number and returns the result.

toBigDecimalOrNull

fun String.toBigDecimalOrNull(): BigDecimal?
fun String.toBigDecimalOrNull(
    mathContext: MathContext
): BigDecimal?

Parses the string as a java.math.BigDecimal number and returns the result or null if the string is not a valid representation of a number.

toBigInteger

fun String.toBigInteger(): BigInteger
fun String.toBigInteger(radix: Int): BigInteger

Parses the string as a java.math.BigInteger number and returns the result.

toBigIntegerOrNull

fun String.toBigIntegerOrNull(): BigInteger?
fun String.toBigIntegerOrNull(radix: Int): BigInteger?

Parses the string as a java.math.BigInteger number and returns the result or null if the string is not a valid representation of a number.

toBoolean

fun String.toBoolean(): Boolean

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

toByte

fun String.toByte(): Byte
fun String.toByte(radix: Int): Byte

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

toByteArray

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.

toByteOrNull

fun String.toByteOrNull(): Byte?
fun String.toByteOrNull(radix: Int): Byte?

Parses the string as a signed Byte number and returns the result or null if the string is not a valid representation of a number.

toCharArray

fun String.toCharArray(): CharArray

Returns a new character array containing the characters from this string.

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.

toCollection

fun <C : MutableCollection<in Char>> CharSequence.toCollection(
    destination: C
): C

Appends all characters to the given destination collection.

toDouble

fun String.toDouble(): Double

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

toDoubleOrNull

fun String.toDoubleOrNull(): Double?

Parses the string as a Double number and returns the result or null if the string is not a valid representation of a number.

toFloat

fun String.toFloat(): Float

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

toFloatOrNull

fun String.toFloatOrNull(): Float?

Parses the string as a Float number and returns the result or null if the string is not a valid representation of a number.

toHashSet

fun CharSequence.toHashSet(): HashSet<Char>

Returns a HashSet of all characters.

toInt

fun String.toInt(): Int
fun String.toInt(radix: Int): Int

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

toIntOrNull

fun String.toIntOrNull(): Int?
fun String.toIntOrNull(radix: Int): Int?

Parses the string as an Int number and returns the result or null if the string is not a valid representation of a number.

toList

fun CharSequence.toList(): List<Char>

Returns a List containing all characters.

toLong

fun String.toLong(): Long
fun String.toLong(radix: Int): Long

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

toLongOrNull

fun String.toLongOrNull(): Long?
fun String.toLongOrNull(radix: Int): Long?

Parses the string as a Long number and returns the result or null if the string is not a valid representation of a number.

toLowerCase

fun String.toLowerCase(): String

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

fun String.toLowerCase(locale: Locale): String

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

toMutableList

fun CharSequence.toMutableList(): MutableList<Char>

Returns a MutableList filled with all characters of this char sequence.

toPattern

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.

toRegex

fun String.toRegex(): Regex

Converts the string into a regular expression Regex with the default options.

fun String.toRegex(option: RegexOption): Regex

Converts the string into a regular expression Regex with the specified single option.

fun String.toRegex(options: Set<RegexOption>): Regex

Converts the string into a regular expression Regex with the specified set of options.

toSet

fun CharSequence.toSet(): Set<Char>

Returns a Set of all characters.

toShort

fun String.toShort(): Short
fun String.toShort(radix: Int): Short

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

toShortOrNull

fun String.toShortOrNull(): Short?
fun String.toShortOrNull(radix: Int): Short?

Parses the string as a Short number and returns the result or null if the string is not a valid representation of a number.

toSortedSet

fun CharSequence.toSortedSet(): SortedSet<Char>

Returns a SortedSet of all characters.

toUpperCase

fun String.toUpperCase(): String

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

fun String.toUpperCase(locale: Locale): String

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

trim

fun String.trim(predicate: (Char) -> Boolean): String

Returns a string having leading and trailing characters matching the predicate removed.

fun String.trim(vararg chars: Char): String

Returns a string having leading and trailing characters from the chars array removed.

fun String.trim(): String

Returns a string having leading and trailing whitespace removed.

trimEnd

fun String.trimEnd(predicate: (Char) -> Boolean): String

Returns a string having trailing characters matching the predicate removed.

fun String.trimEnd(vararg chars: Char): String

Returns a string having trailing characters from the chars array removed.

fun String.trimEnd(): String

Returns a string having trailing whitespace removed.

trimIndent

fun String.trimIndent(): String

Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last lines if they are blank (notice difference blank vs empty).

trimMargin

fun String.trimMargin(marginPrefix: String = "|"): String

Trims leading whitespace characters followed by marginPrefix from every line of a source string and removes the first and the last lines if they are blank (notice difference blank vs empty).

trimStart

fun String.trimStart(predicate: (Char) -> Boolean): String

Returns a string having leading characters matching the predicate removed.

fun String.trimStart(vararg chars: Char): String

Returns a string having leading characters from the chars array removed.

fun String.trimStart(): String

Returns a string having leading whitespace removed.

windowed

fun CharSequence.windowed(
    size: Int, 
    step: Int = 1, 
    partialWindows: Boolean = false
): List<String>

Returns a list of snapshots of the window of the given size sliding along this char sequence with the given step, where each snapshot is a string.

fun <R> CharSequence.windowed(
    size: Int, 
    step: Int = 1, 
    partialWindows: Boolean = false, 
    transform: (CharSequence) -> R
): List<R>

Returns a list of results of applying the given transform function to an each char sequence representing a view over the window of the given size sliding along this char sequence with the given step.

windowedSequence

fun CharSequence.windowedSequence(
    size: Int, 
    step: Int = 1, 
    partialWindows: Boolean = false
): Sequence<String>

Returns a sequence of snapshots of the window of the given size sliding along this char sequence with the given step, where each snapshot is a string.

fun <R> CharSequence.windowedSequence(
    size: Int, 
    step: Int = 1, 
    partialWindows: Boolean = false, 
    transform: (CharSequence) -> R
): Sequence<R>

Returns a sequence of results of applying the given transform function to an each char sequence representing a view over the window of the given size sliding along this char sequence with the given step.

withIndex

fun CharSequence.withIndex(): Iterable<IndexedValue<Char>>

Returns a lazy Iterable of IndexedValue for each character of the original char sequence.

zip

infix fun CharSequence.zip(
    other: CharSequence
): List<Pair<Char, Char>>

Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence.

fun <V> CharSequence.zip(
    other: CharSequence, 
    transform: (a: Char, b: Char) -> V
): List<V>

Returns a list of values built from characters of both char sequences with same indexes using provided transform. List has length of shortest char sequence.

zipWithNext

fun CharSequence.zipWithNext(): List<Pair<Char, Char>>

Returns a list of pairs of each two adjacent characters in this char sequence.

fun <R> CharSequence.zipWithNext(
    transform: (a: Char, b: Char) -> R
): List<R>

Returns a list containing the results of applying the given transform function to an each pair of two adjacent characters in this char sequence.

Companion Object Extension Properties

CASE_INSENSITIVE_ORDER

val String.Companion.CASE_INSENSITIVE_ORDER: Comparator<String>

A Comparator that orders strings ignoring character case.

Companion Object Extension Functions

format

fun String.Companion.format(
    format: String, 
    vararg args: Any?
): String

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

fun String.Companion.format(
    locale: Locale, 
    format: String, 
    vararg args: Any?
): String

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

© 2010–2018 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html