Home » Blog » Kotlin Basics – Kotlin String

Kotlin Basics – Kotlin String

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.

package tejprogramming

fun main(args : Array<String>){

    /**
     * These Strings are Immutable which
     * means they are read-only and
     * unchangeable
     */
    val website = "Tej Sumeru"

    /**
     * This is how we declare long strings
     */
    val longString = """Welcome to
        tejsumeru.com"""


    /**
     * Mutable Strings
     */
    var name = "Suvidha"
    var lName = "Malaviya"
    name = name + " " + lName
    println("Name is: $name")
}

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.

package tejprogramming

fun main(args : Array<String>){

    var firstName = "Suvidha"

    /**
     * String interpolation
     */
    println("String Length: ${firstName.length}")

    /**
     * Or you can display like this
     */
    println("String Length: " + firstName.length)
}

 

Output:

String Length: 7
String Length: 7

 

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 compare To() method.

package tejprogramming

fun main(args : Array<String>){

    var str1 = "TejProgramming"
    var str2 = "tejprogramming"

    /**
     * true if equals, otherwise false
     */
    println("String Equals? : ${str1.equals(str2)}")

    /**
     * 0 if equals, otherwise false
     */
    println("String Equals? : ${str1.compareTo(str2)}")
}

 

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

package tejprogramming

fun main(args : Array<String>){

    var str = "TejProgramming"

    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: P
3rd Index: P

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.

package tejprogramming

fun main(args : Array<String>){

    var str = "TejProgramming"

    /**
     * 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: jPr

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()

package tejprogramming

fun main(args : Array<String>){

    var str = "tejsumeru.com"

    println("Contains .com: ${str.contains(".com")}")
}

Leave a Reply

Your email address will not be published.