Kotlin Strings


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

var firstName = "Mark" // can be changed val 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 templates

String templates are a much easy and nice way of concatenate and generate strings:

var a = 1 // simple name in template: val s1 = "a is $a" println(s1)

Output:

a is 1

The template also allows the use of functions:

a = 2 // arbitrary expression in template: val s2 = "${s1.replace("is", "was")}, but now is $a" println(s2)

Output:

a was 1, but now is 2

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 // Here 910 is a string value. 19

Resources

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.