Last active
August 5, 2018 03:41
-
-
Save madhankumardroid/010a0dd18c5c11261eb1fc98a313c68b to your computer and use it in GitHub Desktop.
Exercise for strings in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str: String = "Hello World!" //With type specification | |
var str1 = "Get started with Kotlin strings" //With out type specification | |
var str2 = str + ", " | |
var emptyString = String()//Creating an empty string | |
fun main(args: Array<String>) { | |
str2 += str1 | |
println(str) //Result: Hello World! | |
println(str1) //Result: Get started with Kotlin strings | |
println(str2) //Result: Hello World!, Get started with Kotlin strings | |
//String Manipulation | |
//Length of the string | |
println("Length of str2: " + str2.length) //Result: Length of str2: 45 | |
println("Character at the index 10 of the string: " + str2.get(10)) //Result: Character at the index 10 of the string: d | |
println("Substring from str1: " + str1.subSequence(16,23)) //Result: Substring from str1: Kotlin | |
//String comparison | |
var strCompare = "Hello WORLD!" | |
println(strCompare.compareTo(str)) //Result: -32 | |
//Using ignoreCase parameter | |
println(strCompare.compareTo(str, true)) //Result: 0 | |
//Accessing characters in a string | |
//Using index operator | |
println("Index operator: Character at 5th position: " + str[4]) //Result: Index operator: Character at 5th position: o | |
//Using get method | |
println("Get method: Character at 5th position: " + str.get(4)) //Result: Get method: Character at 5th position: o | |
//String iteration | |
for(element in str) { | |
println(element) | |
} | |
//String templates | |
var len = str1.length | |
//Inserting a variable | |
println("Length of str1 is $len") //Result: Length of str1 is 31 | |
//Inserting an expression | |
println("Length of str1 is ${str1.length}") //Result: Length of str1 is 31 | |
//To use the $ symbol in a string we need to escape the character | |
var escapedDollar = "Balance is \$45" | |
println(escapedDollar) //Result: Balance is $45 | |
//Raw strings => These are placed inside triple quotes. We don't have to escape characters here. | |
//Used in multilines without concatenation. It is handy while specifying file/directory path | |
var rawString = """Hi Kotlin | |
Where to find your tutorials? | |
I'm eager to learn you.\n""" | |
println(rawString) | |
//Result: Hi Kotlin | |
// Where to find your tutorials? | |
// I'm eager to learn you.\n | |
//trimMargin() to remove the whitespacing. | |
rawString = """Hi Kotlin | |
|Where to find your tutorials? | |
|I'm eager to learn you.\n""".trimMargin("|") | |
println(rawString) | |
//Result: Hi Kotlin | |
//Where to find your tutorials? | |
//I'm eager to learn you.\n | |
//String templates and raw strings | |
var strTemplate = "Hi Kotlin, " | |
var rawStr = """$strTemplate | |
|Your balance is $60.00""".trimMargin("|") | |
println(rawStr) | |
//Result: Hi Kotlin, | |
//Your balance is $60.00 | |
//Overriding String method | |
class Person(var name: String, var age: Int) { | |
override fun toString(): String { | |
return "Person name is $name and age is $age" | |
} | |
} | |
var person = Person("Madhan", 25) | |
println(person) // Result: Person name is Madhan and age is 25 | |
//String equality | |
//Two types | |
//Referential equality(===): Checks the references are same. | |
//Structural equality(==): Checks the contents are equal | |
var s1 = "Kotlin" | |
var s2 = "How are you" | |
var s3 = "Kotlin" | |
var s4 = "Kot" | |
var s5 = "lin" | |
var s6 = s4 + s5 | |
println(s1===s2) // Result: false, s1 and s2 points to different strings | |
println(s1===s3) // Result: true, s1 and s2 points to the same string in the StringPool | |
println(s1==s3) // Result: true, since contents are equal | |
println(s1==s2) // Result: false, since contents are not equal | |
println(s1==s6) // Result: true, since the contents are equal | |
println(s3==s6) // Result: true, since the contents are equal | |
println(s1===s6) // Result: false, since s6 is made up of two different strings. Therefore, s1 and s6 points to different strings | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment