Strings (functions) in kotlin
package com.example.my_kotlin_practice
import android.R
import androidx.compose.ui.text.toUpperCase
fun main(args: Array<String>)
{
val escapedString : String = "I am escaped String!\n"
val rawString : String = """This is going to be a
multi-line string and will
not have any escape sequence"""
print(escapedString)
println(rawString)
// String Templates
// Kotlin string templates are pieces of code that are evaluated and whose results are interpolated into the string.
// A template expression starts with a dollar sign ($) and may consist of either a name or an expression.
val name : String = "Hello brother"
val name2 : String = "bro"
val name4 : String = "Kotlin"
val name5 : String = "Kotlin"
val name6 : String = "Kotlin "
val name7 : String = "Hello Brother2678"
println("Name : $name")
println("Name length : ${name.length}")
println(name[4])
println(name[2])
println("The length of name : ${name.length}")
println("The length of name : " + name.length)
println("The length of name : ${name.count()}")
println("The length of name : " + name.count())
println("The index of last character in name : " + name.lastIndex)
println("Upper case of name : " + name.uppercase())
println("Lower case of name : " + name.lowercase())
var firstname : String = "How are you ?"
var lastname : String = "we will meet tomorrow"
println("Full name : " + firstname + lastname)
println("Full name : $firstname$lastname")
println("Full name : " + firstname.plus(lastname))
println("Remove first two characters from name : " + firstname.drop(2))
println("Remove last two characters from name : " + firstname.dropLast(4))
var str1 : String = "That's it"
var str2 : String = "It's ok"
println("str1 : $str1")
println("str2 : $str2")
var str : String = "Meditation and Yoga are synonymous with India"
println("Index of Yoga in the string : " + str.indexOf("Yoga"))
var str3 : String = "Apple"
var str4 : String = "Apple"
println(str3.compareTo(str4))
println(str1.compareTo(str2))
println(firstname.compareTo(lastname))
println(str3.getOrNull(0))
println(str3.getOrNull(2))
println(str3.getOrNull(200))
println(str3.toString())
// Following is the syntax of the Kotlin string length property
// var length : Int
var myString : String = "This is kotlin string"
var length = myString.length
println(length)
// we display the string if the string length is greater than 10
var myString2 : String = "This is kotlin string"
var length2 = myString2.length
if(length2 > 10)
{
println(myString2)
}
else {
println("string length is smaller than 10")
}
// Following is the syntax of the Kotlin string indices property
// var indices : IntRange
var str5 : String = "tutorialspoint"
for(i in str5.indices)
{
println("Characters at index $i : ${str5[i]}")
}
// another example of the indices property here we print the range of the string
var str6 : String = "This is tutorialspoint"
var range = str6.indices
println("Range of the string : $range")
// below example demonstrates how to perform filtering based on indices
var str7 : String = "tutorialspoint!"
var evenIndexChars = str7.indices.filter { it % 2 == 0 }.map { str7[it] }
println(evenIndexChars)
// Following is the syntax of the Kotlin string get() function
// fun get(index : Int) : Char
// fun get(index: Int) : Char { return 'a'; }
var text : String = "Hello, tutorialspoint"
var charAtIndex : Char = text.get(7)
println("Character at index 7 : $charAtIndex")
// var charAtIndex2 : Char = text.get(100)
// println("Character at index 100 : $charAtIndex2")
var text2 : String = "Hello, world!"
try {
var charAtIndex2 : Char = text2.get(100)
println("Character at index 100 : $charAtIndex2")
}
catch(e: IndexOutOfBoundsException) {
println("Exception caught : ${e.message}")
}
// Following is the syntax of the Kotlin string isEmpty() function
// This function does not accept any parameters
// fun Charsequence.isEmpty() : Boolean
// Following is the syntax of the Kotlin string isNotEmpty() function
// fun CharSequence.isNotEmpty() : Boolean
var string : String = ""
var res : Boolean = string.isEmpty()
val res3 : Boolean = string.isNotEmpty()
if(res)
{
println("String is empty")
}
else {
println("String is not empty")
}
if(res3)
{
println("String is empty")
} else {
println("String is not empty")
}
val userInput : String = "Amar123"
if(userInput.isEmpty())
{
println("Please enter a valid input")
} else {
println("Thank you for your input")
}
val userInput2 : String = "Hello"
val res2 : Boolean = userInput2.isEmpty()
println("Is string empty ? : $res2")
// Following is the syntax of the Kotlin string isBlank() function
// fun isBlank(): Boolean
val str8 : String = " "
val str9 : String = "helo BrotheR"
println(str8.isBlank())
println(str9.isBlank())
// Following is the syntax of the Kotlin string first() function
// fun first(predicate: (Char) -> Boolean) : Char
val first_char = str9.first()
println("First character of string : $first_char")
val firstUpperCase : Char = str9.first { it.isUpperCase() }
println(firstUpperCase)
// Following is the syntax of the Kotlin string last() function
// fun last(predicate: (Char) -> Boolean): Char
// If we do not pass the predicate then this function returns the last character, otherwise, returns
// the last character matching the given predicate
val last_char = str9.last();
println("Last character of string : $last_char")
val LastUpperCase : Char = str9.last { it.isUpperCase() }
println(LastUpperCase)
val lastVowel : Char = str9.last { it in "aeiouAEIOU" }
println(lastVowel)
// The Kotlin string getOrNull() function is used to retrieve the character at the specified index but
// returns null instead of throwing an exception if the index is out of bounds.
// It eliminates the risk of throwing an 'indexOutOfBoundException'
// Following is the syntax of the Kotlin string getOrNull() function
// fun CharSequence.getOrNull(index: Int): Char?
val str10 : String = "tutorialspoint"
val char1 : Char? = str10.getOrNull(1)
println("Character at index 1 : $char1")
val char2 : Char? = str10.getOrNull(20)
println("Character at index 20 : $char2")
val str11 : String = ""
val char3 : Char? = str11.getOrNull(0)
println("Character at index 0 : $char3")
// The Kotlin string equals() function is used to compare the equality of two objects.
// It checks whether the specified object is "equal to" the current string.
// Following is the syntax of the Kotlin string equals() function
// fun equals(other: Any?) : Boolean
val str12 : String = "Kotlin"
val str13 : String = "Kotlin"
val str14 : String = ""
val str15 : String = "Java"
val str16 : String = "Java "
println("Is both strings equals : ${str12.equals(str13)}")
println("Is this string equals : ${str12.equals(null)}")
println("Is this string equals : ${str12.equals(123)}")
println("Is this string equals : ${str12.equals(str14)}")
println(str12.compareTo(str13))
println(str12.compareTo(str14))
println(str12.compareTo(str15))
println(str15.compareTo(str16))
println(str12.compareTo("kotlin", ignoreCase = true))
println(str12.compareTo("kotlin"))
println("Name is : $name")
println(name.startsWith("Hello"))
println(name.startsWith("hello"))
println(name.startsWith("hello", ignoreCase = true))
println(name.startsWith("brother", startIndex = 6))
println(name.startsWith("brother", startIndex = 7))
println(name.startsWith("H"))
println(name.startsWith("h"))
println("Name is : $name")
println(name.endsWith("brother"))
println(name.endsWith("Brother"))
println(name.endsWith("Brother", ignoreCase = true))
println(name.endsWith("r"))
println(name.endsWith("R"))
println("Name is : $name")
println("Name2 is : $name2")
val flag : Boolean = name.contains(name2)
println("Is this substring available : $flag")
val flag2 : Boolean = name.contains("r")
println("Is this character available : $flag2")
println("When case sensitive : $flag2")
val flag3 : Boolean = name.contains("R", ignoreCase = true)
println("When case insensitive : $flag3")
val name3 : String = "Welcome to tutorialspoint, learn Kotlin!"
val regex : Regex = Regex("tutorials")
val result : Boolean = regex.containsMatchIn(name3)
println("Does the string contain the pattern? : $result")
println("Name is : $name")
println("Name2 is : $name2")
println("Name4 is : $name4")
println("Name5 is : $name5")
println("Name6 is : $name6")
val index : Int = name.indexOf(name2,2)
println("First occurrence of string is : $index")
val index2 : Int = name.indexOf("T",4, ignoreCase = true)
println("First occurrence of string is : $index2")
val index3 : Int = name.lastIndexOf("O",10, ignoreCase = true)
println("Last occurrence of character is : $index3")
val index4 : Int = name.lastIndexOf("O",2,ignoreCase = true)
println("Last occurrence of character is : $index4")
println(name4.lastIndexOf(name5))
println(name4.lastIndexOf(name6))
val index5 : Int = name.lastIndexOf(name2,10,true)
println("Last occurrence of string is : $index5")
val index6 : Int = name.lastIndexOf(name2,5,true)
println("Last occurrence of string is : $index6")
val index7 : Int = name.lastIndexOf("L",4,false)
println("Last occurrence of character is : $index7")
val index8 : Int = name.lastIndexOf("L",1,false)
println("Last occurrence of character is : $index8")
println("Name is : $name") //value is "Hello brother"
println("Name2 is : $name2") //value is "bro"
println("Name4 is : $name4") //value is "Kotlin"
println("Name5 is : $name5") //value is "Kotlin"
println("Name6 is : $name6") //value is "Kotlin "
println("Name7 is : $name7") //value is "Hello Brother2678"
val index9 : Int = name.indexOfFirst { it in "aeiou" }
println("index of first vowel : $index9")
val index10 : Int = name7.indexOfFirst { it.isDigit() }
println("index of first digit : $index10")
val index11 : Int = name7.indexOfFirst { it.isUpperCase() }
println("index of first uppercase : $index11")
val index12 : String = name.substring(4,10)
println("Substring : $index12")
val index13 : String = name.substring(4)
println("Substring : $index13")
}
OUTPUT :
I am escaped String! This is going to be a multi-line string and will not have any escape sequence Name : Hello brother Name length : 13 o l The length of name : 13 The length of name : 13 The length of name : 13 The length of name : 13 The index of last character in name : 12 Upper case of name : HELLO BROTHER Lower case of name : hello brother Full name : How are you ?we will meet tomorrow Full name : How are you ?we will meet tomorrow Full name : How are you ?we will meet tomorrow Remove first two characters from name : w are you ? Remove last two characters from name : How are y str1 : That's it str2 : It's ok Index of Yoga in the string : 15 0 11 -47 A p null Apple 21 This is kotlin string Characters at index 0 : t Characters at index 1 : u Characters at index 2 : t Characters at index 3 : o Characters at index 4 : r Characters at index 5 : i Characters at index 6 : a Characters at index 7 : l Characters at index 8 : s Characters at index 9 : p Characters at index 10 : o Characters at index 11 : i Characters at index 12 : n Characters at index 13 : t Range of the string : 0..21 [t, t, r, a, s, o, n, !] Character at index 7 : t Exception caught : Index 100 out of bounds for length 13 String is empty String is not empty Thank you for your input Is string empty ? : false true false First character of string : h B Last character of string : R R e Character at index 1 : u Character at index 20 : null Character at index 0 : null Is both strings equals : true Is this string equals : false Is this string equals : false Is this string equals : false 0 6 1 -1 0 -32 Name is : Hello brother true false true true false true false Name is : Hello brother true false true true false Name is : Hello brother Name2 is : bro Is this substring available : true Is this character available : true When case sensitive : true When case insensitive : true Does the string contain the pattern? : true Name is : Hello brother Name2 is : bro Name4 is : Kotlin Name5 is : Kotlin Name6 is : Kotlin First occurrence of string is : 6 First occurrence of string is : 9 Last occurrence of character is : 8 Last occurrence of character is : -1 0 -1 Last occurrence of string is : 6 Last occurrence of string is : -1 Last occurrence of character is : -1 Last occurrence of character is : -1 Name is : Hello brother Name2 is : bro Name4 is : Kotlin Name5 is : Kotlin Name6 is : Kotlin Name7 is : Hello Brother2678 index of first vowel : 1 index of first digit : 13 index of first uppercase : 0 Substring : o brot Substring : o brother Process finished with exit code 0
Comments
Post a Comment