Published Jan 11, 2023Updated Oct 24, 2023
Contribute to Docs

Strings are immutable objects that represent a sequence of characters contained within double quotes ("").


val stringName: String = "string value"

In Kotlin, string values are always defined as instances of the String class.

String Templates

Templates can also be used to dynamically produce string values. Dynamic values don’t have to start as strings, and can either be directly referenced with a dollar sign ($) or evaluated as a string with additional curly braces ({}).


The following example showcases the various ways strings are used in Kotlin:

fun main(args: Array<String>) {
// String literals
val greeting: String = "Hello, World!"
// String templates
val dynamicValue = 4
val stringOne: String = "The value of dynamicValue is $dynamicValue"
val a = dynamicValue
val b = 5
val stringTwo: String = "The sum of a and b is ${a + b}"

The output for the above code will be:

Hello, World!
The value of dynamicValue is 4
The sum of a and b is 9

String Methods

The following methods are convenient tools for handling a range of string related operations.


Compares two strings lexicographically.
Returns a string where the characters have been removed from the start to the nth index given.
Returns a string with the last n characters removed.
Returns a boolean value representing whether the sequence ends with the specified suffix.
Filters elements from a collection based on a given predicate.
Returns all elements of the original sequence not present in the predicate.
Converts a string to lowercase.
Pads a string using specified characters.
Pads the start of a string with the specified character.
Returns two strings from the characters in the parameter string that satisfy a given predicate.
Adds indentation to a string.
Checks if a specified region within a Kotlin String matches the content of another string.
Removes a specified prefix from a Kotlin string, if present.
Removes a range of characters from a Kotlin string.
Removes a specified suffix from a Kotlin string, if present.

All contributors

Looking to contribute?

Learn Kotlin on Codecademy