Kotlin - Data Type Strings

Introduction

Kotlin strings are immutable.

String literals can be created using double quotes or triple quotes.

Double quotes create an escaped string.

In an escaped string, special characters must be escaped:

Demo

fun main(args: Array<String>) {

        val string = "string with \n new line" 
        //w  ww .  java 2 s.  c om
        println(string)
}

Triple quotes create a raw string.

In a raw string, no escaping is necessary, and all characters can be included:

Demo

fun main(args: Array<String>) {
        val rawString = """ 
        raw string is super 
        useful for strings 
        that span //from ww w  .ja  va 2  s  .c om
        test test test
        many lines """ 
        
        println(rawString)
}

Strings provide an iterator function which can be used in a for loop.