Monday, 27 November 2017

Kotlin: Strings

String is a collection of characters, enclosed in double quotes. Strings in kotlin are defined by using the keyword String and these are immutable.

Ex
var message: String = "Good Morning"

StringsDemo.kt
fun main(args: Array<String>) {
 var message: String = "Good Morning"

 println(message)
}

Output
Good Morning


All the escape sequences (\n, \t, \a etc.,) supported by java are also supported by kotlin.

Ex
var message: String = "Good Morning\nHow are you\nsee you soon"


StringDemo.kt
fun main(args: Array<String>) {
 var message: String = "Good Morning\nHow are you\nsee you soon"

 println(message)

}

Output

Good Morning
How are you
see you soon

If you do not want to perform escaping, then embed the string in """ (triple quotes).


StringDemo.kt

fun main(args: Array<String>) {
 var message: String = """Good Morning\nHow are you\nsee you soon
 
       
       After two lines\bgood bye"""

 println(message)

}

Output

Good Morning\nHow are you\nsee you soon


                                                        After two lines\bgood bye



Previous                                                 Next                                                 Home

No comments:

Post a Comment