String is a sequence of characters. In this guide, we will see how to declare, use and manipulate strings in Kotlin.
Declare a String in Kotlin
There are whole bunch of ways we can define a String in Kotlin. Lets have a look at the following example, here we have declare two immutable strings website
& longString
and we have also declared two mutable strings name
& lName
. This example also shows a simple way of string concatenation, to read more refer String Concatenation in Kotlin guide.
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ /** * These Strings are Immutable which * means they are read-only and * unchangeable */ val website = "BeginnersBook" /** * This is how we declare long strings */ val longString = """Welcome to Beginnersbook.com""" /** * Mutable Strings */ var name = "Chaitanya" var lName = "Singh" name = name + " " + lName println("Name is: $name") }
Output:
Get String Length in Kotlin
Lets see how we can get the String length in Kotlin. In the following example we have a String firstName
and we are displaying the length of this string.
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ var firstName = "Chaitanya" /** * String interpolation */ println("String Length: ${firstName.length}") /** * Or you can display like this */ println("String Length: " + firstName.length) }
Output:
String Length: 9 String Length: 9
Compare Strings in Kotlin
Lets take an example where we compare two Strings in Kotlin. There are two ways to compare Strings, either using equals() method or using compareTo() method.
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ var str1 = "BeginnersBook" var str2 = "beginnersbook" /** * true if equals, otherwise false */ println("String Equals? : ${str1.equals(str2)}") /** * 0 if equals, otherwise false */ println("String Equals? : ${str1.compareTo(str2)}") }
Output:
Access character in a string at a specific index
We can get a character from a specific index in a string using the get() method which is equivalent to the charAt() method of Java
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ var str = "BeginnersBook" println("3rd Index: ${str.get(3)}") /** * Another way of doing the same * This is the recommended way */ println("3rd Index: ${str[3]}") }
Output:
3rd Index: i 3rd Index: i
Substring
We can display a substring in Kotlin using the subSequence()
method. We can provide the fromIndex
and toIndex
in the subSequence(fromIndex, toIndex)
method where fromIndex
is inclusive and toIndex
is exclusive.
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ var str = "BeginnersBook" /** * Here fromIndex is inclusive and * toIndex is exclusive which means * 5th index char will not be included */ println("Index from 2 to 5: " + str.subSequence(2,5)) }
Output:
Index from 2 to 5: gin
Check whether String contains another String
We can use contains() method to check whether the given string contains the specified string or not. Lets take an example to understand the usage of contains()
/** * created by Chaitanya for Beginnersbook.com */ package beginnersbook fun main(args : Array<String>){ var str = "beginnersbook.com" println("Contains .com: ${str.contains(".com")}") }
Output:
Leave a Reply