Member-only story
String in Kotlin
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::classprintln(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''