Working with Strings in Kotlin

The basic datatype of strings in Kotlin is mostly similar as that of used in java. Only the declaration of the variable is different with Kotlin.

String declaration and definition in Kotlin

var myName:String = "lynxbee"

var VARIABLE_NAME:String = "ACTUAL_STRING"

Above is the simple declaration and definition of String in Kotlin.

Concatnating Strings with Kotlin

var myDomain:String = ".com"
myCompleteName = myName + myDomain

NEW_STRING = STRING1 + STRING2

Checking in Another string is present / Identify if substring exists in String

var myDomain:String = ".com"
var myCompleteName = "lynxbee.com"
if (myCompleteName.contains(myDomain)) {
    // this returns true, if domain .com is present in lynxbee.com string
}

Checking if a String starts with another substring

var myCompleteName = "https://lynxbee.com"
if (myCompleteName.startsWith("http")) {
    // this returns true, if domain lynxbee.com is started with http or https
}

Leave a Comment