Kotlin Strings are sequence of charactes similar to java. In kotlin all Strings are objects of String class
Create a String
How we will create a String varibale in kotlin, Example val myStr= "Hello Kotlin String"
here myStr is a varibale of type String.
Read Characters of String
We knows String is seaquesnce of charactes, by using of index we can read charactes.val myStr = "Hello Kotlin String"
val item = myStr[0]
print(item)
here the Output will be H
We can read all characters from string like below
fun main(args:Array)
{
val myStr="Hello"
for(str in myStr)
println(str)
}
Output: H e l l o
Strings are immutable, that means we cannot modify a String by changing some of its elements var myString="Kotlin, New Version"
var myString:String="Kotlin, New Version"
println(myString.hashCode())
var myString2:String="Kotlin, New Version"
println(myString2.hashCode())
var my:StringBuffer = StringBuffer("k");
println(my.hashCode())
var my2:StringBuffer = StringBuffer("k");
println(my2.hashCode())
Output: 780002467 780002467 1360875712 1625635731
-String pool is a special storage area in The Heap Memory. -When a string created and if string already exists in the pool, the reference address of the existing string will be returned, instead of creating a new object and returning its reference. -If string is mutable, changing the string with one reference will give wrong value for other references.
String Properties & Functions
length : This will returns the number charactes contians the String
get(index) : It will returns the character at given index inside the string
subSequence(startindex,endIndex) : This will retunrs the sub string of given string, startIndex is the start position of the substring, and endIndex is the last position to fetch the substring. Here the lastIndex will be the excluded.
compareTo(string): This will retunrs 0 if the both strings are equal value. If first string is greater than second one will return 1 else return negative -1 Similarly if we pass the compareTo(string,true/false) the second arg is wheather ignore case sensitive
val s1 = "Kotlin String"
val s2 = "Kotlin string"
var result: String
println("Length of s1 string is ${s1.length}.")
//Output : Length of s1 string is 13
result = if (s1.compareTo(s2) == 0) "equal" else "not equal"
println("Strings s1 and s2 are $result.")
//Output :Strings s1 and s2 are not equal.
var res=(s1.compareTo(s2,false))
println("Strings s1 and s2 are $res.")
//Output :-32 means s1 lesser than s2
res=(s1.compareTo(s2,true))
println("Strings s1 and s2 are $res.")
//Output :0
// s1.get(2) is equivalent to s1[2]
println("Third character is ${s1.get(2)}.")
//Output : Third character is t.
result = s1.plus(" How are you?")
//Output : result = Kotlin String How are you?
println("result = $result")
//Output : result = Kotlin String How are you?
println("Substring is \"${s1.subSequence(4, 7)}\"")
//Output : Substring is "in "
String Equality
=== This will check the Reference of the both strings, if both are same then it will returns true
== This will check the structure of the both strings, if both are same then it will returns true
var a = "Kotlin"
var b = "Kotlin language"
var c = "Kotlin"
var d1 = "Kot"
var d2 ="lin"
var d = d1 + d2
println(a===c) // true since a and c objects point to the same String in the StringPool
println(a==c) //true since contents are equal
println(a===b) //false
println(a==b) //false
println(a===d) //false since d is made up of two different Strings. Hence a and d point to different set of strings
println(a==d) //true since the contents are equal