Code Mastery Guides

Your Journey to Mastering Programming Languages: Where Knowledge Meets Code

String in Kotlin: A Journey into Text Manipulation

Introduction:

In Kotlin, text manipulation is key for crafting applications. Strings, enclosed in double quotes, are the tools developers use to mold and shape text within the code.

Declaring String:

In Kotlin, declaring a String is straightforward. You don’t need an elaborate syntax; assigning a value to a String variable is as simple as:


    var firstName = "John"

Assigning Values:

To assign value, use the = operator and make sure to use double quotes to value. Assigning values is as simple as:


    first_name = "John"

Make sure to use var rather than val in Kotlin, if you want to edit that value at runtime.

String Interpolation:

In Kotlin, combining two Strings is a breeze with the $ operator. Let’s delve into the fundamentals of String Interpolation in this section. Take a glance at the code below:


    var first_name = "Kumar"
    var last_name = "Wishwash"

    var fullName = "$first_name $last_name"

In the above snippet, we merge two Strings using the $ operator, ensuring it’s enclosed within double quotes. Remember, if you wish to include any method within double quotes, enclose it within curly brackets {}. Let’s understand it with code:


    var first_name = "Kumar"
    var last_name = "Wishwash"
    var age = 15

    var fullName = "$first_name $last_name 's age is ${age.toString()}"

String Manipulations:

You can manipulate String using the following different methods. We will learn, subString, lowerCase/upperCase, length retrieval, replace, compare, etc.


    var sampleString = "This is Sample String"

    val subString = sampleString.substring(0,4)

    println(subString)

In the provided code snippet, a substring of the ‘sampleString’ object is utilized. This code specifically extracts a substring starting from position 0 with a length of 4 characters from ‘sampleString’. Consequently, in the aforementioned code excerpt, the resulting substring will be ‘This’.


    var sampleString = "This is Sample String"

    val lowerCase = sampleString.lowercase()

    val upperCase = sampleString.uppercase()

In the provided code snippet, both the lower-case and upper-case versions of the ‘sample-string’ are utilized.


    var sampleString = "This is Sample String"

    var anotherSample = "This is not a Sample String"

    val length = sampleString.length

    val replaced = sampleString.replace("is", "was")

    val comparison = sampleString.compareTo(anotherSample) //both string are same if return 0

In the provided code snippet, the length of the ‘sample-string’ is utilized, returning an integer value. Additionally, a string replacement operation substitutes occurrences of ‘is’ with ‘was’, and a comparison operation returns 0 if both strings are identical.

String in Kotlin: A Journey into Text Manipulation

Leave a Reply

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

Scroll to top