Lesson 9 - Strings in Kotlin - Split
In the previous exercise, Solved tasks for Kotlin lesson 8, we've practiced our knowledge from previous lessons.
Lesson highlights
Are you looking for a quick reference on split and join in Kotlin instead of a thorough-full lesson? Here it is:
Using String
methods
(substring/compareTo):
{KOTLIN_CONSOLE}
println("I would not banish all of these Internets.".substring(2, 7))
println("alpha".compareTo("bravo"))
{/KOTLIN_CONSOLE}
Using the split()
and
joinToString()
methods:
{KOTLIN_CONSOLE}
var text = "I like Kotlin"
val words = text.split(" ") // split the text by spaces
println("${words[0]} love ${words[2]}") // print the third word
{/KOTLIN_CONSOLE}
Would you like to learn more? A complete lesson on this topic follows.
In the previous lesson, Solved tasks for Kotlin lesson 8, we made clear that String
s
are essentially arrays of characters. In today's Kotlin tutorial, we're going to
explain other String
methods that I have intentionally kept from
you because we didn't know that strings are similar to arrays
We can call many methods which we know from arrays in a similar fashion. For
example: first()
, last()
, indexOf()
and
others.
When you create an arbitrary variable and write a dot after it, IntelliJ will show us all of the available methods and variables, that we can call on that variable (we'll go deeper into this in the OOP course). Let's try it out:

The same suggestion can also be accessed by pressing Ctrl + Spacebar when the text cursor is on the dot. Of course, this applies to all variables and classes (we'll use it further along the way, as well). The methods are ordered alphabetically and we can list them using the arrow keys. IntelliJ shows us the description of the methods, what they do, and what parameters do they need.
Let's talk about the following methods and demonstrate them on simple examples:
Additional String methods
substring()
Returns a substring from the given start position to the given end position.
println("I would not banish all of these Internets.".substring(2, 7))
The output:
would
compareTo()
It allows us to compare two strings alphabetically. Returns -1
if the first string is before the string in the parameter, 0
if
they are equal and 1
if the string is after one in the
parameter:
println("alpha".compareTo("bravo"))
The output:
-1
Now let's look at one more, very useful, String
method.
split()
and
joinToString()
From the previous tutorial, we know that parsing strings character by character can be rather complicated. Even though we made a fairly simple example. Of course, we'll encounter strings all the time, both in user inputs, e.g. from the console or from input fields in form applications, and in TXT and XML files. Very often, we're given one long string, a line in a file or in the console, in which there are multiple values separated by separators, e.g. commas. In this case, we're talking about the CSV format (Comma-Separated Values). To be sure that we all know what we're talking about, let's look at some sample strings:
Jessie,Brown,Wall Street 10,New York,130 00 .. ... .-.. .- -. -.. ... --- ..-. - (1,2,3;4,5,6;7,8,9)
- The first string clearly represents a user. We could, for example, store users into a CSV file (one per line).
- The second string is Morse code characters and uses the space character as the separator.
- The third string is a matrix of 3 columns and 3 rows. The column separator is a comma, whereas the row separator is a semicolon.
We can call the split()
method on a String
, which
takes a separator. It'll then split the original string using separators into an
array of substrings and return it. This will greatly simplify value extraction
from strings for our current intents and purposes.
The joinToString()
method, on the other hand,
merges an array of substrings using a separator into a single string. The
parameter is the separator. The output of the method is the resulting string.
The method can be called without a parameter, thus joining strings without
separators.
Right then, let's see what we've got up until now. We still don't know how to declare objects, users, or even work with multidimensional arrays, i.e. matrices. Nevertheless, we want to make something cool, so we'll settle with making a Morse code message decoder.
Morse code decoder
We'll start out by preparing the structure of the program, as always. We need
two strings for the messages, one for a message in Morse code, the other one
will be empty for now and we'll store the results of our efforts there. Next, we
need letter definitions (as we had with vowels). Of course, we'll also need the
Morse code versions of the letter definitions. We can store letters as a single
String
since they only consist of one character. The Morse code
letters consist of multiple characters, therefore we have to specify them using
an array.
The structure of our program should now look something like this:
// the string which we want to decode val s = ".. -.-. - ... --- -.-. .. .- .-.." println("The original message: $s") // the string with the decoded message var message = "" // array definitions val alphabetChars = "abcdefghijklmnopqrstuvwxyz" val morseChars = arrayOf(".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..")
We could also add other Morse characters such as numbers and punctuation
marks, but we won't worry about them for now. We'll split the String
s
with the split()
method into an array of substrings
containing the Morse characters. We'll split it by the space character. Then
we'll iterate over the array using a for
loop:
// splitting the string into Morse characters val characters = s.split(" ") // iterating over Morse characters for (morseChar in characters) { }
Ideally, we should somehow deal with cases when the user enters e.g. multiple
spaces between characters (users often do things of the sort). In this case,
split()
creates one more empty substring in the array. We should
then detect it in the loop and ignore it, but we won't deal with that in this
lesson.
In the loop, we'll attempt to find the current Morse character in the
morseChars
array. We'll be interested in its index
because when we look at that same index in the alphabetChars
string, there will be the corresponding letter. This is mainly because both the
array and the string contain the same characters which are ordered
alphabetically. Let's place the following code into the loop's body:
val index = morseChars.indexOf(morseChar) // character was found if (index != -1) { message += alphabetChars[index] }
We try to find the Morse code character index. If we succeed, we find the
corresponding letter in the alphabet and we add it to the message. The
+=
operator works the same as
message = message + alphabetChars[index]
.
Now, we'll print the message:
println("The decoded message: $message")
The output:
The original message: .. -.-. - ... --- -.-. .. .- .-.. The decoded message: ictsocial
Done! If you want to train some more, you can create a program which would
encode a string to the Morse code. The code would be very similar. We'll use the
split()
and joinToString()
methods several more times
throughout our Kotlin courses.
Special characters and escaping
Strings can contain special characters which are prefixed with backslash
\
. Mainly, the \n
character, which causes a line break
anywhere in the text, and \t
, which is the tab character.
Let's test them out:
println("First line\nSecond line")
The \
character indicates a special character sequence in a
string and can be used also e.g. to write Unicode characters as
\u{xxxx}
where xxxx is the character code.
The problem might be when we want to write \
itself, in this
case we've to escape it by writing one more \
:
println("This is a backslash: \\")
We can escape a quotation mark in the same way, so Kotlin wouldn't misinterpret it as the end of the string:
println("This is a quotation mark: \"")
Inputs from the console and input fields in form applications are, of course,
escaped automatically, so the user doesn't need to enter \n
,
\t
, etc.. Programmers are allowed to write these characters in the
code, so we have to keep escaping in mind.
Raw string
Sometimes we need to write multiple backslashes in one string. If we escaped
all of them, it could make our code unclear. Because of this problem, we can
declare a Raw string in Kotlin. We'll just wrap the text into """
and all backslashes will behave like ordinary characters.
"""I don't have to escape anything in this string \"""
Today we basically finished the on-line course on the Kotlin language basic
constructs. In the next lesson, Solved tasks for Kotlin lesson 9, we'll look at a bonus episode about
multidimensional arrays and we'll briefly talk about the Math class. Nothing
will surprise you from the basic language constructs anymore In fact, you could potentially
start working with objects now, but I would suggest for you to read the next few
lessons. You all still have a long way to go, but your future looks bright!
In the following exercise, Solved tasks for Kotlin lesson 9, we're gonna practice our knowledge from previous lessons.
Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.
Download
By downloading the following file, you agree to the license terms
Downloaded 5x (8.58 kB)
Application includes source codes in language Kotlin