Now let’s take two big steps forward in our journey in computer science.
First, we’ll learn how to work with text data in Kotlin, using a data type called a String
.
String
s also represent another step forward, since they are the first type that we will work with as a Kotlin object.
We’ll spend a lot of time discussing objects in future lessons, so this lesson is our first taste of what is yet to come.
Language is one of the things that makes humans special. And, while many animals communicate through vocalizations, written language is even more unique to our species. Words and text have long played an incredibly important role in human societies. So clearly this is a kind of data that we want to be able to work with in our computer programs.
Happily, Kotlin has a special data type specifically for working with text:
Note how String
s differ from char
s, in that they are enclosed in double quotes (”) rather than single quotes (’):
Kotlin String
s are not limited to the limited number of characters that we can store in a char
:
A full discussion of Unicode and how characters are represented in modern programming languages is outside the scope of this class. But it’s a fascinating story with lots of interesting wrinkles. Safe to say, we have fully overcome the limitations of early programs ability to work with non-latin alphabets. Unicode even includes emoji:
(Note that the in-browser editor gets a bit weird around emoji, probably because they aren’t the same width as other characters.)
One useful thing that we can do with String
s is combine them.
Kotlin allows us to do this using the +
operator:
However, in Kotlin it is usually more convenient to use String
interpolation.
We’ve already seen this in action in our println
examples:
String
interpolation allows us to create new Strings
that include the values
of variables, which are prefixed by a $
, as shown above.
This is quite convenient, and we’ll prefer it to String
concatenation.
(It is also more powerful in ways that we haven’t explored quite yet!)
Let's get some practice working with Kotlin String
s: an incredibly useful data type for working with text.
Write a function called reformatPhoneNumber
.
It should take a String
containing a phone number in the format 111-222-3333
and return it reformatted as
(111) 222-3333
.
You will want to explore the various
String
methods
to help you with this task.
In particular, you may find split
and substring
helpful.
There are solutions that use split
, others that use substring
, and probably others that use neither!
String
s as ObjectsString
s as ObjectsOn one hand, String
s just seem like any other Kotlin variable.
But there is something new going on here.
Let’s explore together:
String
s are ObjectsString
s are ObjectsThe unusual behavior that we observed above is due to the fact that String
is not one of the eight basic Kotlin types.
In fact everything in Kotlin—including the basic types—is an object!
Even the basic types have methods that we can call:
We’ll be talking a lot about objects in future lessons, but for now we’ll define an object as something that combines state and behavior, or data and functionality.
Kotlin objects can be seen as uniting two of the basic building blocks that we’ve already been exploring: variables and methods.
Like a variable, Kotlin objects store information.
String
s store a series of characters.
But in addition, Kotlin objects also come with built-in methods that we can call!
Frequently, those methods operate on the data contained in the object.
Let’s look at how that works out with String
s, our first object.
Note that this is a screencast, rather than a walkthrough, so that we can consult some documentation together!
The best way to familiarize yourself with these features is to browse the official String
documentation.
Over the set of homework problems on String
s that start on this lesson, we may expect you to use some of their built-in features, and point you at the relevant documentation.
To call a method on a String
, we use so-called dot notation.
Let’s explore that in the following walkthrough:
Write a function called emailToNetID
.
It should take a String
containing an @illinois.edu
email like [email protected]
and return the NetID,
which in this case would be "hello", as a String
.
You can assume that the passed String
will contain a single @
character.
And you should require
that the passed String
ends with @illinois.edu
.
You will want to explore the various String
methods to help you with this task, particularly split
.
Need more practice? Head over to the practice page.