In this tutorial you will learn about Strings via simple step by step examples.

Example 1: Strings

Let us look at a simple Strings example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

Kotlin Android SharedPreferences

Kotlin Android SharedPreferences
Kotlin Android SharedPreferences
  • Strings.kt
  1. In your editor or IDE, create a file known as Strings.kt.
  2. Then add the following code:

(a). Strings.kt

package Strings

fun main(args : Array<String>) {
    var plato1 = "Wise men speak because they have something to say; Fools because they have to say something."

    //This is a single line string
    println("I love ancient philosophy.")

    //Use triple quote """ to go multi line. This also means that you can use double quote " freely in the string.
    //This demonstrate template expression. Pretty neat huh. You can use ${ } to show any arbitrary expression
    println("""My favorite quote is by '$plato1' although at ${plato1.length} characters long, it is not compact.
He is also a skeptic on love "${platoOnLove()}. He is wrong, love is as simple as 1 + 1 = ${1 + 1 }."

fun platoOnLove() : String {
    return "Love is a serious mental disease."

/* what's next? try Variables\Variables.kt to learn about immutable/mutable variable declarations */

Step 4: Run

Copy the code, build and run.


Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License