A string is just a sequence of strings, enclosed in double quotes. In Kotlin, strings are represented by types. It is one of the most widely used data types in practice.
1. The length of the string
You can get the number of characters in the string accessing the property. This property has a value of type. length Int
val language = "Kotlin" println(language.length) // 6 val empty = "" println(empty.length) // 0
2. Access characters
The elements of a string are single characters that can be accessed by their index. The first element of the string has index 0 .
val greeting = "Hello" val first = greeting // 'H' val second = greeting // 'e' val five = greeting // 'o'
The index of the last element is equal to the length of the string minus 1 . For strings, the last element is . Its index is 4 because the length of the string is 5 .
val last = greeting[greeting.length - 1] // 'o' val prelast = greeting[greeting.length - 2] // 'l'
Kotlin provides several convenience methods for accessing the first and last characters of a string:
println(greeting.first()) // 'H' println(greeting.last()) // 'o' println(greeting.lastIndex) // 4
You can use this feature to write more readable code.
Strings are immutable, meaning, once created the string is always the same. The elements of the string cannot be modified. So the example below doesn't work:
val str = "string" str = 'o' // an error here!
But this works:
var str = "string" str = "strong"
Actually, we don't modify the stored value in the variable. Instead, we assign it a new value. So it's absolutely legal. This is one of the ways to use strings. If you need to modify the string, just create a new one.
4. Connection String
Two strings can be concatenated using operators:
val str1 = "ab" val str2 = "cde" val result = str1 + str2 // "abcde"
When we concatenate two strings, a new string is created (since strings are immutable).
Multiple strings can be concatenated in the same expression:
val firstName = "John" val lastName = "Smith" val fullName = firstName + " " + lastName
Note, not the same, because concatenation is not a commutation operation (unlike addition).
Append value to string
This operator also works for appending values of different types to strings. The value is automatically converted to a string and then concatenated to the target string.
val str = "abc" + 10 + true println(str) // abc10true val code = "123" + 456 + "789" println(code) // 123456789
Expressions must always start with a string, see the example below. It doesn't work because the first operand is a number:
val str = 10 + "abc" // an error here!
The next one might surprise you:
val str = "abc" + 11 + 22 println(str) // abc1122
Why is this happening? Because first it appends to the string and then appends to the string.
5. Compare strings
To compare two strings, use the (equal) and (not equal) operators. Both operators take two strings as operands and return a value of type (or ).
val first = "first" val second = "second" var str = "first" println(first == str) // true println(first == second) // false println(first != second) // true