Skip to content

Kotlin kotlin.String API

Kotlin's String — an immutable sequence of UTF-16 code units, enhanced with extension functions.

1 class · 8 methods

String

8 methods

Represents a character sequence. Kotlin String is immutable and bridges to java.lang.String.

String.length: Int

Returns the length of this character sequence.

Returns

Int

Example

kotlin
val s = "hello"
println(s.length)  // 5
String.uppercase(): String

Returns a string with all characters converted to uppercase using default locale rules.

Returns

String

Example

kotlin
val s = "Hello World"
println(s.uppercase())  // "HELLO WORLD"
String.lowercase(): String

Returns a string with all characters converted to lowercase using default locale rules.

Returns

String

Example

kotlin
val s = "Hello World"
println(s.lowercase())  // "hello world"
String.split(vararg delimiters: String): List<String>

Splits this string around matches of the given delimiters.

Parameters

NameTypeDescription
delimitersStringDelimiter strings to split on.

Returns

List<String>

Example

kotlin
val parts = "a,b,c".split(",")
// parts == ["a", "b", "c"]
val two = "a-b-c".split("-", limit = 2)
// two == ["a", "b-c"]
String.replace(oldValue: String, newValue: String): String

Returns a new string with all occurrences of oldValue replaced with newValue.

Parameters

NameTypeDescription
oldValueStringSubstring to replace.
newValueStringReplacement string.

Returns

String

Example

kotlin
val s = "Hello, World"
val r = s.replace("World", "Kotlin")
// r == "Hello, Kotlin"
String.contains(other: CharSequence): Boolean

Returns true if this string contains the specified char sequence.

Parameters

NameTypeDescription
otherCharSequenceSubstring to search for.

Returns

Boolean

Example

kotlin
val s = "Hello, World"
println(s.contains("World"))  // true
println(s.contains("xyz"))    // false
String.trim(): String

Returns a string with leading and trailing whitespace removed.

Returns

String

Example

kotlin
val s = "  hello  "
println(s.trim())  // "hello"
String.startsWith(prefix: String): Boolean

Returns true if this string starts with the specified prefix.

Parameters

NameTypeDescription
prefixStringPrefix to check.

Returns

Boolean

Example

kotlin
val s = "Hello, World"
println(s.startsWith("Hello"))  // true
println(s.startsWith("World"))  // false