Kotlin String Example

This example shows a simple Kotlin string example. Strings are a sequence of characters. For example, "Hello there!" is a string literal.

In Kotlin, all strings are objects of String class. Meaning, string literals such as "Hello there!" are implemented as instances of this class.

Kotlin String Example

The example creates a string, uses a string concatenation operation, and determines the width of the string.
package net.javaguides.kotlin.examples

fun main(args: Array<String>) {
 
 val s = "Source Code Examples"

 println(s)

 println("sourcecodeexamples" + ".net")

 println("The string has " + s.length + " characters")
}
Output:
Source Code Examples
sourcecodeexamples .net
The string has 20 characters
A string literal is created and passed to the s variable. The string is printed to the console with println().
val s = "Source Code Examples"

println(s)
In Kotlin, strings are concatenated with the + operator:
println("sourcecodeexamples" + ".net")

Comments