Kotlin has five basic data types. In a previous post, we discussed the first basic type in Kotlin, numbers. This time, the discussion will be related to strings.
In Kotlin, like in most programming languages, strings are groups of characters that form what we normally use as words, even though this is not necessarily true, as strings can have a group of characters that don’t make any sense and still be the same type.
The syntax for strings is easy, just wrap your characters between double quotes and you will have a string:
val string = "Hello, world!"
Properties of Strings
1. They are immutable
In programming, when we talk about “immutability”, we refer to the inability to change an object. Take, for example, the following String:
var myDescription = "I am a programmer"
There is no way to alter that String, we don’t have a function where you can tell the code: “After the sixth character of the variable, insert the word great.” If you wanted to change the string you would need to reassign it.
myDescription = "I am a great programmer"
Important note: It is a common beginner confusion to think that the above statement is a mutation — it is not.
What happens with the above line is that we are “reassigning” the variable, which means we are destroying the previous value it had and assigning it a new one, which could be similar or completely different.
2. Strings can be iterated and accessed via indexing
Strings in Kotlin behave just like an array would, you can access them through a zero-index logic:
var anotherString = "I like programming in Kotlin" anotherString[3] // This would give the character 'i' as a result
Also, you could check each character with a loop:
for (letter in anotherString) {
println(letter)
}
Pro tip: Iterating through strings in the above fashion is very useful in algorithmic problems, which are very common in coding interviews.
3. Strings can be concatenated
This is not a recommended practice, but it is a feature of Kotlin Strings. If you use the +
operator, you can concatenate, which is the same as “joining” two Strings together.
var name = "Evana"
var lastName = "Margain" var fullName = name + " " + lastname
// result: "Evana Margain"
Now you should be thinking: “This looks very useful, why is it not recommended?”
There are other ways to achieve the same thing. Actually, in programming, most tasks can be achieved through different methods. The recommended method is called String templates.
4. String templates
If you have ever used the word template in another context, you know that a template is a base format for something, in this case, a String. Let’s take a look at an example of a String template:
val language = "Kotlin" println("My favorite language is $language") val anotherLanguage = "Swift" println("My favorite language is $anotherLanguage")
Can you guess the results of the above code? The first line would print “My favorite language is Kotlin,” while the second one would print “My favorite language is Swift”.
If you observe carefully, the way to insert a variable into the string is by adding a dollar sign before the variable name. Easy, isn’t it?
If you want to add one variable to another, remember you should always prefer String templates over concatenation.
5. String literals: escaped Strings
When you are using Strings, sometimes you have to use characters that may confuse the compiler, for example:
val famousQuote = ""Be yourself; everyone else is already taken." ― Oscar Wilde"
If the computer tries to interpret the code above, it will think you are closing your String, while in reality, you want to put a double quote as part as your String.
The above code is not valid, but making it work is easy, just add a backslash behind the character.
val famousQuote = "\"Be yourself; everyone else is already taken.\" ― Oscar Wilde"
Now the compiler will know that you are trying to add a double quote, and will search for the closing quote that doesn’t have a backslash before it.
According to the Kotlin documentation, the available escape sequences are: \t
, \b
, \n
, \r
, \'
, \"
, \\
, and \$
. But you don’t have to learn this, there is an even easier method, take a look at the next part.
6. String literals: raw Strings
Another way you can include characters that would normally be a part of code is through raw Strings. Let’s look at an example:
"""
"Be yourself; everyone else is already taken." ― Oscar Wilde
"""
As you can see in the above code, raw strings begin with three quotes and end with another three, just make sure you put the quotes on a line of their own.
Now you can put any character between them. If you add a break, it will also be respected. This is a feature not many programming languages have, take advantage of it! Sometimes, trying to escape many characters gets annoying and frustrating.
Congratulations! you can see, Kotlin is a very easy language, but “with great power comes great responsibility,” so don’t give up on your Kotlin learning path! If you want to learn even more on this topic, I recommend this article on Guru99.com which has even more resources for your learning path.
Until next time!