Overview
String
is one of the basic type in any programming languages, including Kotlin. I have never seen any application or program that does not deal with String
. Have you ever? ๐ค
That's why I thought we should spend some time looking at the fundamentals of String
. In this tutorial, we will learn the basics of String
in the Kotlin programming language.
Initialise a String Object
Let's start by learning how to initialise a String
object. To initialise a String object, simply wraps your expression with double quotes "
.
val myString = "This is my first String object"
myString::class
println(myString)
// val myString: String
// class kotlin.String
// This is my first String object
Note that the expression myString::class
gives us the instance type of an object.
Unlike in some other languages like Python, we cannot create a String
object with single quotes '
. If we try to do so, we will get a compilation error.
val myStringWithSingleQuotes = 'This is my first String object'
// Too many characters in a character literal ''This is my first String object''
This is because in Kotlin, String
is a sequence of UTF-16 code units and a single UTF-16 code unit has the type of Char
. In other words, String
in Kotlin is just a collection of multiple Char
. There's a nice reference here if you're interested.
This leads to the next point.
String is a Sequence of Char
As mentioned in the previous section, String
is simply a sequence or collection of characters or Char
objects. A collection is indexable, right? So, this means, we can access any of the characters in a String
object.
val indexString = "indexable"
println(indexString[0])
println(indexString[3])
// i
// e
We also know that a collection is iterable. So, characters in a String
object can be iterated over in a for
loop operation.
val iterString = "iter"
for (char in iterString) {
println(char)
}
// i
// t
// e
// r
String Literals
In Kotlin, there are two types of string literals:
- an escaped string with some escaped character in it
- a raw string
Escaped string
Let's look at some examples of escaped strings.
val escapedString = "Hello\nworld"
println(escapedString)
val anotherEscapedString = "Hi\tthere"
println(anotherEscapedString)
// val escapedString: String
// Hello
// world
// val anotherEscapedString: String
// Hi there
Our escapedString
contains a newline character \n
and we can see how it gets printed in the console. The anotherEscapedString
has a tab character \t
and again, it can be seen in the console output.
Raw string
Similar to Python, we can construct a raw string object by wrapping our string in triple double quotes """
. Inside these quotes, we can have any character we'd like and do not need to escape it.
val rawString = """
This is multiline
without newline
character
"""
println(rawString)
// This is multiline
// without newline
// character
As you can see, with this raw string literal, we can add newlines without having to insert the newline character \n
.
Furthermore, notice the outcome of our rawString
. It has some indentation, right? There's a built-in method that you can use if you want to get rid of those indents.
val rawString = """
This is multiline
without newline
character
""".trimIndent()
println(rawString)
// This is multiline
// without newline
// character
See how the indents are removed now? ๐
String Concatenation
Now, let's take a look at how we can combine or concatenate String
objects together. There are two ways to concatenate strings:
- using plus operator
+
- using string template
Plus operator (+)
Again, just like in Python, we can concatenate multiple strings by using the +
operator.
val stringOne = "I"
val stringTwo = "am"
val stringThree = "learning"
val stringFour = "Kotlin"
val space = " "
val concatenateString = stringOne + space + stringTwo + space + stringThree + space + stringFour
print(concatenateString)
// val stringOne: String
// val stringTwo: String
// val stringThree: String
// val stringFour: String
// val space: String
// val concatenateString: String
// I am learning Kotlin
String template
String template is basically a string literal that contains some variables or expressions, which will be evaluated and finally, concatenated into the string.
To do this, we need to prepend a variable with the dollar sign $
and if it's an expression, the expression needs to be wrapped in curly braces {expression}
.
val stringOne = "I"
val stringTwo = "like"
val stringThree = "Kotlin"
val templateString = "$stringOne $stringTwo ${stringThree.toUpperCase()}"
println(templateString)
// val stringOne: String
// val stringTwo: String
// val stringThree: String
// val templateString: String
// I like KOTLIN
Personally, I much prefer to use string template because it's more readable and elegant. Again, that's just personal preference. ๐
Wrap Up
Well done to you for learning the basics of String
in Kotlin. I hope that this tutorial has given you a good understanding of what and how a String
object is in Kotlin.
For next time, we will take a look at the methods of String
so we can learn how to manipulate it when we need to.
Feel free to drop any comments or questions that you have and I will try to answer it as soon as possible. ๐