String Data Types in Kotlin

Start course
1h 43m

This course covers the fundamentals of Kotlin, looking at the building blocks of the programming language and how they can be used to build apps in Android.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.


This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.


Well, hello there, my friends.  On this lesson, we will learn String Data Types. Now, by string in common, we mean a set of characters, such as the word "Hello" or the phrase "Kotlin is funny". When passing a value to a variable of string data type, we write the value to be passed between double quotation marks. So, if you remember from before, we had a data type called CHAR in our data types lesson. This CHAR data type could only store one character. But what if we want to pass an expression with more than one character to a variable? That's why we need to use the variable type called String.

Now in our example, it represents a string object in memory consisting of "Hello" characters. So, characters in a string are identified by their index number. In "Hello" in fact, the leftmost character 'H' is at index zero, next character 'E' is at index one, and then it continues numerically up from there. So, the string class provides many useful methods to perform string operations. So, have a look at the table. The length method returns the length of a string, it has no parameters. The equals method compares this string with the specified string and returns true if two string objects contain the same contents, else false.

Now, the isEmpty method returns true if the given string has zero as its length. If the length of the string is non-zero, then it returns false. Plus method adds a string expression enclosed in parenthesis to the end of another desired string expression. Lowercase method returns a string in lowercase. Uppercase method returns a string in uppercase. Now, the trim method removes spaces at the beginning and end of the word. Cool. All right, so, we'll move over to Android Studio and warm up with some practice.

So, before creating a new Kotlin file, let's just close these files we created in previous lessons because now I want to create a new Kotlin file. I'll specify the name of this file to be string data type. All right. So, we'll create a main method, and then I'll create a variable of type string. So, I'm writing var a:String here. After typing equals, you can type in any expression that you want to between double quotation marks. So, I'm just typing in "Hello Kotlin". And now, let's print the variable a to the console. So, I'm writing printIn(a) and now let's run the code.

Okay so, as you see the console says "Hello Kotlin". Now, I want to show you how we can concatenate to a string variable. So for this, I will create a new variable of the type string. And here, I write var b:String; I write language between double quotes. Now, let's print the variables a and b to the screen together. So, I'm typing 'printIn' here and write (a+b) in parenthesis. So, the plus sign that we use here will concatenate two strings. So, now we can run the application and let's have a look. And sure enough, it says Hello KotlinLanguage on the console. But notice here that there's no space between the two words, right? Because this concatenation method adds the second word directly to the end of the first word. So, how do you put a space between the two words?

I'm glad you asked. I will show you. Since space will actually be of a type string, we can use double quotes and leave one space. So, in parenthesis, (a+" "), I leave a space, plus b. Let's run our code one more time, and there you have it. There is now a space between the words Hello Kotlin and Language. So, what we did here was concatenated two different string expressions. Now, let's print two variables of type string and integer to the console. So, for this I will create a new variable of the type, integer. I write var age:lnt and pass the value 25. Now, let's print your age is 25 on the console. So, after typing 'printIn', I write "Your age is" in parenthesis between the double quotation marks and after leaving one space, I write '+ age'. Now, let's run our code. So as you can see, it says Your age is 25 on the console. Cool. I could see that you were getting it right as we were going along. So, we're going to be using this a lot. Okay? Very, very, very often. So, we can learn some of the methods that are used in the string class. All right?

So first of all, I'll just convert the codes that I wrote here into a comment line. Now, we'll create a new string variable:  'var_x: String =' and I'm writing 'Kotlin.' So now, let's use the length method first. Now, if you remember, the length method gives us the size of a string variable. So now, I want x.length on a bottom line. Of course this method will give us the length of the Kotlin word in the integer type. So, if you press the control and space keys on the keyboard after you move the cursor to the right of the length method, well, you can open a suggestion window here. What do you know? So you see, therefore, it would be more logical to pass this value to a variable of type, integer. That's why I'm typing, 'var i: Int =' here. Thus, the length of the Kotlin word will be calculated with a length method and then passed to the variable i of the type, integer. And finally, we can print this i variable to the console. So, I'm writing 'printIn' in parenthesis and between double quotation marks, I write '"Length of Kotlin ="' and I write '"+i"'.

Cool. So, now let's run the code. And as you can see, the length of the Kotlin word on the console screen is right here, that is, how many characters it consists of. So, that's how you use the length method. Now, let's have a look at how you use the equals method. So, what I'll do is convert the codes here to the comment line but I will not delete the x variable, we can use that. So, I'm typing 'x.equals.' I'm typing in parenthesis between double quotes in Java. So, if the value of the variable x is equal to the Java word, it will give us the value true. If not, it will give us the value false.

So as a result, it's going to return a Boolean value. Right? Therefore, I will pass this result to a variable of type Boolean. So here, I write '"var b: boolean"'. Now, let's print variable b to the console. So, I write b in parenthesis. Okay, so now, let's run and test it out. So, as you can see, it prints the false statement in the console. So now, we can compare two different string expressions using the equals method. All right. So yes, there's more. Let's have a look at isEmpty method. So, this method also checks whether a value is passed to a string variable and if not, it returns false. I'm also converting the codes here in the comments, and now I'm typing '"printIn"' here and I'll write '"x.isEmpty"' in parenthesis. This way we can print the true or false value directly to the console. And as you can see, we saw the false value in the console because the variable x is not empty and the value Kotlin is passed into it.

So now, let's have a look at the plus method. You can concatenate two string type variables using the plus method. Right? So, I'm typing '"printIn"' right here, writing '""' in parenthesis. And we should write an expression of type string inside these parenthesis, so I'm typing '"language"' here. Cool. So now, let's run our code. And as you can see, the Kotlin language  is written in the console. So now, let's have a look at the lowercase and uppercase methods. These methods also convert all letters in a string type expression to lowercase or uppercase.

So, why don't we test it out? So here, I am typing '"printIn"', I write '"x.lowercase"' inside the parenthesis. Now, I'm going to the bottom line after typing '"printIn"', I write '"x.uppercase"'. And also to separate these two lines of code, we can apply a method like this. I'm typing '"printIn"' right here and I'm doing a partition by pressing the asterisk key right on the keyboard between the double quotes and parentheses. So, you can use this method when you're not going to print multiple data to the console.

So now, let's run our code and you can see, first, all letters of the word Kotlin are printed in the lowercase, and then all letters are capitalized. And see both expressions are separated by asterisks. Okay so, finally we'll learn the trim method and we can wrap this lesson up. Of course you remember, trim method was also for removing spaces at the beginning and at the end of string expressions. For example, let's say you made an application that asks the user for username and password. Well, the user may have accidentally put a space at the beginning or the end of the username. People do that. So in this case, if you do not delete this space while storing the username, the user may encounter an error the next time that they log into the application, won't be able to log in, and then they'll go crazy.

Okay. So, in these cases the trim method is used. So, let's now reinforce this issue with an example. So here, I'll just create a new variable of the type string. And I'm writing '"var t:String"'. After double quotes, I put three spaces and type 'Android'. So after typing Android, I'll leave three spaces again. Now, let's print the variables x and t together to the console. First, I will print without using the trim method. It's all right, x+t in parentheses. Just

the bottom line this time, I will use the trim method. So, here I am typing 'x+t.trim'. All right. So now, let's run our code. And look at that. As you can see, it was first printed with spaces, and then it was printed after it removed the spaces in between. What do you think of that? Some kind of magic? I don't think so. That's Kotlin. All right, so my friends, that is how you deal with string variable types. So, we'll take a short break here, and in our next lesson, we're going to learn about the Array Variable Type. All right? There's a lot packed into arrays. So, I'll see you next time.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics