BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin String

Last Updated: February 21, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

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:
Kotlin String

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:
Compare Strings in Kotlin

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:
Kotlin string contains

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Keywords, Soft Keywords and Identifiers
  2. Kotlin Class and Objects – Object Oriented Programming (OOP)
  3. Kotlin Nested and Inner Class with examples
  4. Kotlin Abstract Class with examples
  5. Kotlin Constructors with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap