Kotlin String (original) (raw)
Last Updated : 18 May, 2025
An array of characters is called a string. Kotlin strings are similar to Java strings but have some newly added functionalities. They are also **immutable, which means we can not change the elements and length of the String.
The String class in Kotlin is defined as:
class String : Comparable, CharSequence
To declare a string in Kotlin, we need to use double quotes(" "); single quotes are not allowed to define Strings.
**Syntax:
var variable_name = "Hello, Geeks"
or
var variable_name : String = "GeeksforGeeks"
**Creating an empty String:
To create an empty string in Kotlin, we need to create an instance of the String class.
var variable_name = String()
String Elements and Templates
**String Element
The character, digit, or any other symbol present in the string is called an element of a String. We can easily access the element of the string using string[index]. Elements are stored in a string from index 0 to (string.length - 1).
There are three ways in which you can access string elements in Kotlin
- **Using index: Returns the character at the specified index.
- **Using the **get function: Returns the character at the specified index passed as an argument to the get function.
- **Iterating over the String: Using loops to access the characters in the String.
**Kotlin program to access the elements of a string
Kotlin `
fun main(args: Array){ // accessing string // elements one by one var str = "Hello" println(str[0]) println(str[1]) println(str[2]) println(str[3]) println(str[4]) // accessing the string // elements using for loop var str2 = "Geeks" for(i in str2.indices){ print(str2[i]+" ") } }
`
**Output:
H
e
l
l
o
G e e k s
**String Template
String template expression is a piece of code that is evaluated and its result is returned into string. Both string types (escaped and raw string) contain template expressions. String templates starts with a dollar sign $ which consists of either a variable name or an arbitrary expression in curly braces.
Kotlin `
fun main(args: Array) { var n = 10 println("The value of n is $n") // using string val str = "Geeks" println("$str is a string which length is ${str.length}") }
`
**Output:
The value of n is 10
Geeks is a string which length is 5
String length
**length: Returns the length of the String.
Kotlin `
var s =" String" println(s.length)
`
String get an element by index
**get(index): Returns the character at that particular index.
Kotlin `
s.get(3) // Output: - i
`
String subSequence
**subSequence(start, end): Returns a substring starting from start and ending at end but excluding end.
Kotlin `
s.subSequence(1, 4) // Output: - tri
`
String CompareTo
**str.compareTo(string): Returns 0 if str == string.
Kotlin `
var s1 = "GeeksForGeeks" var s2 = "GeeksForGeeks" s1.compareTo(s2) // Output:- 8
`
**Parameters:
- **s1: String 1 for comparison
- **s2: String 2 for comparison
**Returns:
- if **string1 > string2, it returns **positive number
- if **string1 < string2, it returns negative number
- if **string1 == string2, it returns **0
**Kotlin program using the above properties and functions
Kotlin `
fun main(args: Array) { var g = "GeeksForGeeks" var e = "Geeks" println(g.length) println(g.get(4)) println(g.subSequence(0, 5)) println(g.compareTo(e)) }
`
**Output:
13
s
Geeks
8
String Literals
There are two types of string literals in Kotlin -
- Escaped String
- Raw String
**1. Escaped String
Escaped string is declared with double quotes ("....") and it may contain escape characters like \n, \t etc.
**Kotlin program of escaped string:
Kotlin `
fun main(args: Array) { // escaped string val str = "World \n is \n amazing" println(str) }
`
**Output:
World
is
amazing
**2. Raw String - Multi-line String
Raw string is placed inside the triple quotes ("""....""") and it does not have escape characters. It provides the facility of writing the string into multiple lines****,** so it is also called a multi-line string.
**Kotlin program of raw string:
Kotlin `
fun main(args: Array) { // raw string - multiline string var str = """My |name |is |Yash """.trimMargin() println(str) }
`
**Output:
My
name
is
Yash
**Escape Characters - Some of the escape characters are:
- \" : for double quote
- \r : for carriage return
- \n : for newline
- \' : for single quote
- \\ : for backslash
- \t : for tab
- \b : for backspace
String Equality
Kotlin provides an additional feature of comparing the instances of a particular type in two different ways. This feature makes Kotlin different than the other programming languages.
The two types of equality are
- Structural Equality
- Referential Equality
**Structural Equality
Structural equality is checked through the **== operator and its inverse ****!= operator**. By default, the expression containing **x==y is translated into the call of the **equals() function for that type.
**Referential Equality
The referential equality in Kotlin is checked through the **=== operator and its inverse ****!== operator**. This equality returns true only if both instances of a type point to the same location in memory. When used on types that are converted into a primitive type at runtime, the === check is converted into == check, and !== check is converted into != check.
**Kotlin program to demonstrate the structural and referential equality
Kotlin `
fun main(args: Array) { var x = "GeeksForGeeks" var y = "GeeksForGeeks" var z = "Geeks" println(x===y) // true , as both are pointing to the same StringPool println(x==z) //false since values are not equal println(x===z) // false }
`
**Output:
true
false
false