⇒ Strings
- Strings in Kotlin are represented by the type
String- Generally, a string value is a sequence of characters in double quotes (" "):
val str = "123"
- Strings are immutable. Once you initialize a string, you can't change its value or assign a new value to it.
- All operations that transform strings return their results in a new String object, leaving the original string unchanged:
- Elements of a string are characters that you can access via the indexing operation:
s[i].
- You can iterate over these characters with a for loop:
fun main() { val str = "abcd" for (c in str) { println(c) } }
- An array of characters is called a string.
- Kotlin strings are mostly similar to Java strings but has some new added functionalities.
- Kotlin strings are also immutable in nature means we can not change elements and length of the String.
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 String class.
var variable_name = String()
- Using index: Returns the character at specified index.
- Using get function: Returns the character at specified index passed as argument to get function.
- Iterating over the String: Using loops to access the characters in the String.
Some important properties and functions in String:
- Length: Returns the length of the String.
var s =" String" println(s.length)
- get(index): Returns the character at that particular index.
s.get(3) // Output: - i
- subSequence(start, end):Returns a substring starting from start and ending at end but excluding end.
s.subSequence(1, 4) // Output: - tri
- str.compareTo(string): Returns 0 if str == string.
Kotlin program of using the above properties and functions –
fun main(args: Array<String>) { var g = "GeeksForGeeks" var e = "Geeks" println(g.length) println(g.get(4)) println(g.subSequence(0, 5)) println(g.compareTo(e) }
String literals –
There are two types of string literals in Kotlin –
- Escaped String
- Raw String
- Escaped String
Escaped string is declared with double quotes (“….”) and it may contain escape characters like /n, /t etc.
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
- 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 multi-line string.
fun main(args: Array<String>) { // raw string - multiline string var str = """My |name |is |Yash """.trimMargin() println(str) }
String Equality –
The two types of equality are –
- 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
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 the instances of a type point to the same location in memory. When used on types that are converted into primitives type at runtime,
the === check is converted into == check and !== check is converted into != check.