Kotlin Strings


We said the basic types in Kotlin include:

  1. Numbers.
  2. Booleans.
  3. Chars.
  4. Strings.
  5. Arrays.

We talk about strings in this tutorial.

Strings are used to hold sequences of characters.

  • Kotlin strings are immutable like in most programming languages.
  • Strings belong to type String.
  • You can use double quotes or tripple quotes to create String literals. The difference is that Strings with double quotes can be escaped while those with tripple create raw strings.

String Literals

//new line escaping
val string = "string with \n new line"

//raw string with tripple quotes
val rawString = """
raw string is super useful for strings that span many lines """
  • In Kotlin strings can also be iterated through via a for-loop.
  • Then the individual String elements can be accessed by the indexing operation string[i].
for (c in str) {
    println(c)
}

EXAMPLE

fun main(args: Array<String>) {
    val name="Sophie"
    for (c in name) {
        println(c)
    }
}

RESULT

S
o
p
h
i
e

Kotlin String Templates

In Java, developers have always been using String concatenation to pass expressions inside String literals.

val name="Oclemy"
val greeting="Hello "+name

EXAMPLE

fun main(args: Array<String>) {
    val name="Matilda"
    val greeting="Hello "+name

    println(greeting)
}

RESULT

Hello Matilda

However, we can do this in an easier and more convenient way in Kotlin by using String templates.String templates are normally used when you want to:

  1. Embed values and variables inside a String literal.
  2. Embed expressions inside a string literal.

String templating is now supported in majority of modern programming languages like Javascript,C#,Python and Ruby as it's something we frequently need. It can also be called String interpolation.
String templates are more readable than String concatenation especially when you need to work with multiple variables in a single string literal.

You simply embed a value to astring literal by prefixing with the dollar($) symbol.

var name="Oclemy"
var greeting="Hello $name"

EXAMPLE

fun main(args: Array<String>) {
    var name="Davy"
    var greeting="Hello $name"

    println(greeting)
}

RESULT

Hello Davy

You can add more expressions by prefixing with dollar sign and wrapping in braces:

val name="Oclemy"
val greeting="Hello $name. My name has ${name.length} characters"

EXAMPLE


fun main(args: Array<String>) {
    val name="Oclemy"
    val greeting="Hello $name. My name has ${name.length} characters"

    println(greeting)
}

RESULT

Hello Oclemy. My name has 6 characters

MORE STRING TEMPLATE EXAMPLES

EXAMPLE : String templates with Variable Expressions

Not only can string templates in Kotlin include simple variables but they can also include expressions.

//STRING TEMPLATE EXAMPLE
fun main(args: Array<String>) {
    val spacecraft = "ENTERPRISE"

    //simple variable template
    println("Which spacecraft? $spacecraft")
    // string template with variable expression
    println("Which spacecraft? ${spacecraft.toLowerCase()}")
}

RESULT

Which spacecraft? ENTERPRISE
Which spacecraft? enterprise

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post