Kotlin String

The string is a sequence of characters enclosed within double and triple quotes. It is immutable in nature means we can not change elements and length of the String. Kotlin strings are mostly similar to the Java strings. But unlike Java, Kotlin does not require a new keyword to instantiate an object of a String class. It is represented by the type String and simply declared within double quote (" ") known as escaped string or triple quote(""" """) known as raw string.

6. Kotlin String 6.1 Create Empty String 6.2 Get String Index 6.3 String Iteration 6.4 String Templates 6.5 String Equality 6.6 Get Sub String 6.7 Kotlin Raw String 6.8 Kotlin String Plus


The syntax to define the String class in Kotlin:
class String : Comparable, CharSequence
Examples of Kotlin Strings
var var_name = "Welcome John!"   
or
var var_name : String = "etutorialspoint"


Create Empty String

To create an empty string in Kotlin, we need to create an instance of String class.

var var_name = String()


Get String Index

In Kotlin, elements store in a string from index 0 to (string.length - 1). We can easily access an element of a string using string[index].

fun main(args : Array<String>) 
	var a = "Hello, World"
	println(a[0])
	println(a[3])
	println(a[5])
}
Output
H
l
,

String Iteration

We can iterate over a string using for loop.

fun main(args : Array<String>) 
	var str = """This is envisioned and developed to meet basic and free courses to learn, succeed and belong."""
	for (s in str) {
		println(s)
	}
}
Output
H
e
l
l
o
 
W
o
r
l
d
.




String Templates

In Kotlin, string can contain template expressions, means we can evaluate the code string. It is a piece of code which is evaluated and its result is returned into string. This string expression starts with dollar sign $ which consists either a variable name or an arbitrary expression in curly braces.

Example of String Template

var i = 5
println("i = $i")

The string template arbitrary expression is written in curly braces. In the given example, we are getting the length of the string.

fun main(args : Array<String>) 
	var i = "etutorialspoint"
	println("$i.length is ${i.length}")
}
Output
etutorialspoint.length is 15

If there is a requirement to include a literal dollar sign in a string, a backslash is used.

val str = "\$str" // evaluates to "$str"

String Equality

The string structure equality is compared with the double equality operator.

fun main(args : Array<String>) 
	val str1 = "Welcome, Somya!"
	val str2 = "Welcome," + " Somya!"
	println(str1 == str2) // output: true
}




Get Sub String

Kotlin provides subSequence() function to return a substring between the specified start and end index.

Syntax

subSequence(startIndex, endIndex)

Example of Kotlin substring

fun main(args : Array<String>) 
	val str = "Welcome, Somya!"
	println(str.subSequence(0,6)) // Output : Welcome
}


Kotlin Raw String

The raw string is multiline string placed inside triple quotes ("""..."""). There is no need to escape characters in raw string. As, it provides the facility of writing the string into multiple lines so it is also called multi-line string.

Example of Kotlin raw string

fun main(args : Array<String>) 
    val str = """Welcome, Somya!
    you are learning
    kotlin programming language"""
	println(str)
}
Output
Welcome, Somya!
    you are learning
    kotlin programming language

Kotlin String Plus

In Kotlin, the plus() function returns a new string by concatenating strings.

Example of Kotlin plus

fun main(args : Array<String>) 
	val str = "Welcome, Somya!"
	var str1 = "How are you!"
	var result: String
	result = str.plus(str1)
	println($result)
}
Output
Welcome, Somya! How are you!




Read more articles


General Knowledge



Learn Popular Language