Kotlin Strings
Kotlin Strings
Strings are used for storing text.
A string contains a collection of characters surrounded by double quotes:
Unlike Java, you do not have to specify that the variable should be a String
. Kotlin is smart enough to understand that the greeting variable in the example
above is a String
because of the double quotes.
However, just like with other data types, you can specify the type if you insist:
Note: If you want to create a String
without assigning the value (and assign the value later), you must specify the type while declaring the variable:
Access a String
To access the characters (elements) of a string, you must refer to the index number inside square brackets.
String indexes start with 0. In the example below, we access the first and third element in
txt
:
Example
var txt = "Hello World"
println(txt[0]) // first element (H)
println(txt[2]) // third element (l)
Try it Yourself »
[0] is the first element. [1] is the second element, [2] is the third element, etc.
String Length
A String in Kotlin is an object, which contain properties and functions that can perform certain operations on strings,
by writing a dot character (.
) after the specific string variable. For example, the length of a string can be found with the length
property:
Example
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
println("The length of the txt string is: " + txt.length)
Try it Yourself »
String Functions
There are many string functions available, for example toUpperCase()
and toLowerCase()
:
Example
var txt = "Hello World"
println(txt.toUpperCase()) // Outputs "HELLO WORLD"
println(txt.toLowerCase()) // Outputs "hello world"
Try it Yourself »
Comparing Strings
The compareTo(string)
function
compares two strings and returns 0 if both are equal:
Example
var txt1 = "Hello World"
var txt2 = "Hello World"
println(txt1.compareTo(txt2)) // Outputs 0 (they are equal)
Try it Yourself »
Finding a String in a String
The indexOf()
function returns the index (the position)
of the first occurrence of a specified text in a string
(including whitespace):
Example
var txt = "Please locate where 'locate' occurs!"
println(txt.indexOf("locate")) // Outputs 7
Try it Yourself »
Remember that Kotlin counts positions from zero.
0 is the first position in a
string, 1 is the second, 2 is the third ...
Quotes Inside a String
To use quotes inside a string, use single quotes ('
):
String Concatenation
The +
operator can be used between strings to add them together to make a new
string. This is called concatenation:
Example
var firstName = "John"
var lastName = "Doe"
println(firstName + " " + lastName)
Try it Yourself »
Note that we have added an empty text (" ") to create a space between firstName and lastName on print.
You can also use the plus()
function to concatenate two strings:
Example
var firstName = "John "
var lastName = "Doe"
println(firstName.plus(lastName))
Try it Yourself »
String Templates/Interpolation
Instead of concatenation, you can also use "string templates", which is an easy way to add variables and expressions inside a string.
Just refer to the variable with the $
symbol:
Example
var firstName = "John"
var lastName = "Doe"
println("My name is $firstName $lastName")
Try it Yourself »
"String Templates" is a popular feature of Kotlin, as it reduces the amount of code. For example, you do not have to specify a whitespace between firstName and lastName, like we did in the concatenation example.