Kotlin - Array And String

Introduction

 
In this part, we are going to learn about Arrays and Strings in Kotlin. Those who are willing to learn about Kotlin from basics, click this link.
 
 

Kotlin Array

 
An array is a collection of similar data types either of Int, String, etc. Array in Kotlin is mutable in nature with fixed size which means we can perform both read and write operations, on the elements of an array. Arrays in Kotlin are able to store multiple values of different data types. Of course, if we want we can restrict the arrays to hold the values of particular data types.
 

Kotlin Array Declaration

 
Kotlin arrays can be created using arrayOf(), intArrayOf(), charArrayOf(), booleanArrayOf(), longArrayOf(), shortArrayOf(), byteArrayOf() functions.
 
Example
  1. var myArray1 = arrayOf(1,11,21,31,41)  
  2. var myArray2 = arrayOf<Int>(1,11,21,31,41)  
  3. val myArray3 = arrayOf<String>("Abu","Praveen","Sathya","Yogesh","Ram")  
  4. var myArray4= arrayOf(1,10,4"Abu","Shithik")  

Elements of array

 
Kotlin has set() and get() functions that can directly modify and access the particular element of the array respectively. In Kotlin Array, the get() function is used to get the elements from the specified index. The set() function is used to set element at particular index location.
 
set() function example 
  1. fun main(args: Array<String>) {    
  2.    val array1 = arrayOf(1,2,3,4)    
  3.    val array2 = arrayOf<Long>(11,12,13,14)    
  4.    array1.set(0,5)    
  5.    array1[2] = 6    
  6.    array2.set(2,10)    
  7.    array2[3] = 8    
  8.    for(element in array1){    
  9.       println(element)    
  10.    }    
  11.    println()    
  12.    for(element in array2){    
  13.       println(element)    
  14.    }    
  15. }   
Output
 
5
2
6
4
11
12
10
8
 
get() function Example
  1. fun main(args: Array<String>) {    
  2.    val array1 = arrayOf(7,8,9,0)    
  3.    val array2 = arrayOf<Long>(21,22,23,24)    
  4.    println(array1.get(0))    
  5.    println(array1[2])    
  6.    println()    
  7.    println(array2.get(2))    
  8.    println(array2[3])    
  9. }    
Output
 
7
9
23
24
 

Kotlin String 

 
The String class represents an array of char types. Strings are immutable which means the length and elements cannot be changed after their creation. Unlike Java, Kotlin does not require a new keyword to instantiate an object of a String class. A String can be simply declared within double quote (" ") known as escaped string or triple quote(""" """) known as a raw string.
 
 

String elements

 
The characters which are present in the string are known as elements of a string. The elements of the string are accessed by indexing operation, i.e., string[index]. String's index value starts from 0 and ends at a value less than the size of the string, i.e., string[string.length-1]. Index 0 represents the first element, index 1 represents the second element and so on.
 
Example
  1. fun main(args: Array<String>) {  
  2.    val str = "Csharpcorner"  
  3.    println(str[0])  
  4.    println(str[1])  
  5.    println(str[str.length-1])  
  6. }  
Output
 
C
s
r
 

String templates

 
String template expression is a piece of code which is evaluated and its result is returned into a string. Both string types (escaped and raw string) contain template expressions. String templates start with a dollar sign $ which consists either a variable name or an arbitrary expression in curly braces.
 
Example
  1. fun main(args: Array<String>) {  
  2. val i =10  
  3. print("i = $i")//i=10  
  4. }  
Output
 
i=10
 

Kotlin String Literals

  • Escaped String
  • Raw String
Escaped String
 
Escape String is declared within double quote (" ") and may contain escape characters like '\n', '\t', '\b' ,'\r','\$'etc.
 
Raw String
 
Raw String is declared within triple quote (""" """).It provides the facility to declare String in new lines and contains multiple lines. Row String cannot contain any escape character.
 
Example 
  1. fun main(args: Array<String>) {  
  2.    val text = """Kotlin is the official language  
  3.    announce by Google for  
  4.    Android application development  
  5.    """  
  6.    println(text)  
  7. }  
Output
 
Kotlin is the official language
announce by Google for
Android application development
 

Kotlin String Equality 

 
In Kotlin, strings equality comparisons are done on the basis of structural equality (==) and referential equality (===).
  • In structural equality, two objects have separate instances in memory but contain the same value.
  • Referential equality specifies that two different references point to the same instance in memory.
Structural equality (==) 
 
To check the two objects containing the same value, we use == operator or != operator for negation.
 
Example
  1. fun main(args: Array<String>) {  
  2.    val str1 = "Hello, World!"  
  3.    val str2 = "Hello, World!"  
  4.    println(str1==str2) //true  
  5.    println(str1!=str2) //false  
  6. }  
Output
 
true
false
 
Referential equality (===)
 
To check the two different references point to the same instance, we use the === operator. The !== operator is used for negation. a === b specifies true if and only if a and b both point to the same object.
 
Example
  1. fun main(args: Array<String>) {  
  2.    val str1 = buildString { "string value" }  
  3.    val str2 = buildString { "string value" }  
  4.    println(str1===str2)  
  5.    println(str1!==str2)  
  6. }  
Output
 
false
true
 

Conclusion

 
In this article, we learned about arrays and strings in Kotlin. Next in this series, we will learn Exception Handling, Null safety, and Ranges.
 
Next in this series: Kotlin Exception Handling 


Similar Articles