Strings

A string is a basic data type in a programming language. Strings are represented by the type String. Strings are immutable. Kotlin has a rich API for working with strings.

Basic Usage

Declaration

// Explicit type declaration
var firstName : String = "Elon"

// or Implicit type declaration and will still compile
val lastName = "Musk"

In addition, notice the usage of val variable type, here is how it behaves

firstName = "Mark" // can be changed
lastName = "Zuckerberg" // cannot be changed
lastName = 12 // Error: type mismatch

String Concatenation

Shown in the code snippet, just like Java, appending Int to String will result to a String output

var str = "abc" + 1
println(str + "def")

Output:

abc1def

Even without explicitly converting Int value 1 to String object first, the resulting output is still a String.

String with Multiple Lines

Programmers can declare String variables with multiple lines by using triple quotes instead of double quotes

var str = """
        This is line 1
        This is line 2
        This is line 3
        """
println(str)

Output:

        This is line 1
        This is line 2
        This is line 3

or with .trimIndent()

The use of trimIndent() will additionally help to provide a clean output format by removing excess and unnecessary indentions on each line. Examine the code snippet below:

var str = """
        This is line 1
        This is line 2
        This is line 3
        """.trimIndent()
println(str)

Output:

This is line 1
This is line 2
This is line 3

Accessing Characters of a String

Index Access

Programmers can access elements (characters) of a string using index access operator:

var str = "Example"
println(str[2])

Output:

a

It’s just like accessing an element from an array, you get:

var str = "Example"
println(str[9]) // Error: index out of bounds

Iterate through a String

Elements of a string are characters that can be accessed by the indexing operation: s[i].

var str = "Example"
for (c in str) {
    println(c)
}

Output:

E
x
a
m
p
l
e

Immutability of a String

Just like Java, you cannot change individual elements of a String

var str = "Example"
str[2] = "b" // Error

Re-assigning String values

var str = "Example"
println(str)
str = "Example was changed"
println(str)

Output:

Example
Example was changed

String Properties

Determining length of a String

var str = "Example"
println(str.length)

Output:

7

String Functions

These are some of the common String functions available from the current Kotlin version

compareTo

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.

var str = "Example"
var str2 = "Example123"
var str3 = "Example12345"
println(str.compareTo(str2))
println(str.compareTo(str3))
println(str3.compareTo(str))
println(str.compareTo("Example"))

Output:

-3
-5
5
0 # Equal

equals

Indicates whether a String object is exactly equal to another String object

var str = "Example"
var str2 = "Example2"
println(str.equals("Example"))
println(str2.equals("Example"))

Output:

true
false

get

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

var str = "Example"
println(str.get(3))

Output:

m

toString

Returns a string representation of the object.

println(9.toString() + 10)
println(9 + 10)

Output:

"910"
19

Resources