Kotlin Char & String Usage Examples | Android Introduction #9

ibrahimcanerdogan
3 min readJan 17, 2024

--

Text is a very important part of everyday life, from books to newspapers, instant messages to advertisements. In the world of mobile applications, text is not only important for displaying buttons and labels but also for sending and receiving data. As such, there needs to be a way to represent text when programming.

In Kotlin, this is done using two basic types: Char and String, which you’ll learn more about in this reading.

UDEMY COURSE

Char

Char represents a single character. This can be a letter, a number, punctuation or another symbol. A Char value can be specified in single quotes. For example, to define a Char variable named myChar with the value set to the letter a, you would use the following code:

val myChar: Char = 'a'

String

A String represents a sequence of characters, such as a word or sentence. A String value can be specified in double quotes. For example, to define a String variable named myString with the value set to the word “Hello”, you would use the following code:

val myString = "Hello"

If your text contains multiple lines, it is also possible to define this in a String variable in Kotlin by delimiting the text with triple quotes.

val myString = """Hello
This is a String.
It is on multiple lines.
"""

It is also possible to insert the value of variables into a String value. This is called a String template in Kotlin.

val age = 70 
val myString = "My age is $age"

A template expression starts with the $ symbol. In the above example, the resulting String will be “My age is 70”.

Converting a Char to a String

To convert a Char into a String, use the toString() function.

val myChar: Char = 'a'
val myString = myChar.toString()

Appending to a String

To add additional text to the end of a String, use the + operator. It is important to note that this does not change the existing String variable but creates a completely new String variable.

val myString = "Hello"
//myString is still Hello after this operation.
val myLongStrong = myString + " World"

The following example shows how new variables are created when a String is modified.

fun main() {
var s1 = "Hello"
val s2 = s1 // s1 and s2 now point at the same string - "Hello"
println(s1) // prints "Hello"
println(s2) // prints "Hello"
s1 += "world" // append "world" to s1
println(s1) // prints "Hello world"
println(s2) // still prints "Hello" because s2 still points to the String "Hello" while s1 is a new String object
}

Checking the length of a String

It is possible to check how long a String is using the length property. For example, the length of the String value Hello will return 5.

val myString = "Hello"
val myStringLength = myString.length

This means the myStringLength will have a value of 5.

Searching Strings

The String type has multiple functions for searching its contents. Below are examples.

True if the String starts with Hel, otherwise false

val myString = "Hello"
val startsWithHel = myString.startsWith("Hel")

True if the String ends with lo, otherwise false

val myString = "Hello"
val endsWithLo = myString.endsWith("lo")

Get the first character

val myString = "Hello"
val firstChar = myString.first()

Get the last character

val myString = "Hello"
val lastChar = myString.last()

True if the String is equal to Hello, otherwise false

val myString = "Hello"
val equalsHello = myString.equals("Hello")

There are many more search functions available in the String type. Information on these functions can be found in the Additional Resources at the end of this lesson.

Manipulating Strings

The String type also has multiple functions for manipulating its contents.

Convert the String to uppercase

val myString = "Hello"
val myUpperString = myString.uppercase()

Convert the String to lowercase

val myString = "Hello"
val myLowerString = myString.lowercase()

Get all characters from the second character onwards

Note: Counting starts at zero. myString2 will have the value ello.

val myString = "Hello"
val myString2 = myString.substring(1)

It is important to note that Strings are immutable. This means that when you use a function that manipulates the String, it actually returns a new String instance.

İbrahim Can Erdoğan

LINKEDIN

YOUTUBE

UDEMY

--

--

ibrahimcanerdogan
ibrahimcanerdogan

Written by ibrahimcanerdogan

Hi, My name is Ibrahim, I am developing ebebek android app within Ebebek. I publish various articles in the field of programming and self-improvement.

No responses yet