In this article, we are going to learn about Strings and Ranges in Kotlin with string methods.
StringsKotlin provides different string methods which help us to write our programs faster and in an efficient way.
- equals() method
In Kotlin, to compare two strings, we use equals() methods.For example:
- /**
- * equals method
- */
- var name: String = "Kishor"
- if (name.equals("kishor")) {
- println("They are equal")
- } else {
- println("They are not equal")
- }
- //Output: They are not equal.
It is case sensitive so strings “Kishor” and “kishor” are not equal.
If we want to compare without taking case sensitive into consideration, then we can pass "true" as a parameter in equals method like:
- if (name.equals("kishor", true)) {
- println("They are equal")
- } else {
- println("They are not equal")
- }
- //Output: They are equal.
- toInt() method
In Kotlin, we can convert String into integer using toInt() method.
For example:
- /**
- * String to Integer
- */
- var a: String = "4"
- print(a.toInt());
- //Output: 4
- substring() method
We can get the substring of String. We can print the first name using substring method. For example:- /**
- *Substring
- */
- var myName: String = "Kishor Bikram Oli"
- println(myName.substring(0..5)) //this means 0 to 5
- //Output: Kishor
- String templates
In Java, we can print the name like below:- /**
- * String template
- */
- var name: String = "Kishor"
- println("My name is " + name) //Java
But in Kotlin, we can use String using String templates like:
println("My name is $name") //Kotlin
- contains() method
In Kotlin, if we want to check if some character is present in the string, then we can use contains() methods.For example:
- /**
- * Contains method
- */
- var a1: String = "Hello"
- var a2: String = "World"
- if (a1.contains('e')) {
- println("true")
- } else {
- println("false")
- }
- //Output: true
- replace() method
In Kotlin, replace() method replaces the old char with the new char.For example:
- /**
- * replace method
- */
- var b1: String = "apple"
- println(b1.replace("pp", "tt"))
- //Output: attle
- replaceAfter() method
In Kotlin, replaceAfter() method replaces part of the string after the given character with the new given string.
For example:
- /**
- * replaceAfter method
- */
- var b2: String = "elephant"
- println(b2.replaceAfter('p', "rr"))
- //Output: eleprr
Here, there is ‘hant’ after ‘p’ and it is replaced by ‘rr’, so it becomes ‘eleprr’
- replaceAfterLast() method
In Kotlin, replaceAfterLast() method replaces part of the string after the occurrence of last given character with the new given string.For example:
- /**
- * replaceAfterLast method
- */
- var b3: String = "androidkotlin "
- println(b3.replaceAfter('o', "yyyy"))
- //Output: androidkoyyyy
Here, there is ‘tlin after last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘androidkoyyyy’
- replaceBefore() method
In Kotlin, replaceBefore() method replaces part of the string before the given character with the new given string.For example:
- /**
- * replaceBefore method
- */
- var b2: String = "elephant"
- println(b2.replaceBefore('p', "rrr"))
- //Output: rrrphant
Here, there is ‘ele’ before ‘p’ and it is replaced by given ‘rrr’, so it becomes ‘rrrphant’
- replaceBeforeLast() method
In Kotlin, replaceBeforeLast() method replaces part of the string before the occurrence of last given character with the new given string.For example:
- /**
- * replaceAfterLast method
- */
- var b2: String = "androidkotlin "
- println(b2.replaceBeforeLast('o', "yyyy"))
- //Output: yyyyotlin
Here, there is ‘androidk’ before last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘yyyyotlin’
- reversed() method
In Kotlin, reversed() method gives the string in reversed format.
For example:- /**
- * reversed method
- */
- var d1: String "android"
- println(d1.reversed())
- //Output: diordna
- startsWith() method
In Kotlin, startsWith() method returns true if the string starts with the given prefix.
For example:- /**
- * startsWith method
- */
- var d2: String = "android"
- if (d2.startsWith("an")) {
- println("true")
- } else {
- println("false")
- }
- //Output: true
- endsWith() method
In Kotlin, endsWith() method returns true if the string ends with the given prefix.
For example:- /**
- * endsWith method
- */
- var d3: String = "android"
- if (d3.startsWith("id")) {
- println("true")
- } else {
- println("false")
- }
- //Output: true
Complete code snippet for Strings
- package com.example.kotlin_helloworld
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- class MainActivity: AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle ? ) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- /**
- * Strings
- */
- var name: String = "Kishor"
- /**
- * String template
- */
- if (name.equals("kishor")) {
- println("They are equal")
- } else {
- println("They are not equal")
- }
- //Output: They are not equal. Since it is case sensitive.
- //If you want to compare without taking case sensitive into consideration then we can pass
- // "true" as parameter
- if (name.equals("kishor", true)) {
- println("They are equal")
- } else {
- println("They are not equal")
- }
- //Output: They are equal. Since it is not case sensitive this time.
- /**
- * String to Integer
- */
- //We can convert String into integer
- var a: String = "4"
- print(a.toInt());
- //Output: 4
- /**
- *Substring
- */
- //We can get the substring of string
- var myName: String = "Kishor Bikram Oli"
- //We can print the first name using substring method
- println(myName.substring(0. .5)) //starting from zero index to index 5
- //Output: Kishor
- //String template
- //In Java, we can print name like below:
- println("My name is " + name) //Java
- //But in Kotlin, we can use String template like:
- println("My name is $name") //Kotlin
- //Contains
- //In Kotlin, if we want to check if some character is in the string, then we can use contains methods like below.
- /**
- * Contains method
- */
- var a1: String = "Hello"
- var a2: String = "World"
- if (a1.contains('l')) {
- println("true")
- } else {
- println("false")
- }
- //Replace
- //In Kotlin, replace method replaces the old char with the new char like below:
- /**
- * replace method
- */
- var b1: String = "apple"
- println(b1.replace("pp", "tt"))
- //Output: attle
- //ReplaceAfter
- //In Kotlin, replaceAfter method replaces part of the string after the given character with the new given string.
- /**
- * replaceAfter method
- */
- var b2: String = "elephant"
- println(b2.replaceAfter('p', "rr", b2))
- //Output: eleprr
- //Here, there is ‘hant’ after ‘p’ and it is replaced by ‘rr’, so it becomes ‘eleprr’
- /**
- * replaceAfterLast method
- */
- // In Kotlin, replaceAfterLast method replaces part of the string after the occurrence of last given character with the new given string.
- var b3: String = "androidkotlin "
- println(b3.replaceAfterLast('o', "yyyy"))
- //Output: androidkoyyyy
- //Here, there is ‘tlin after last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘androidkoyyyy’
- //In Kotlin, replaceBefore method replaces part of the string before the given character with the new given string.
- /**
- * replaceBefore method
- */
- var c1: String = "elephant"
- println(c1.replaceBefore('p', "rrr"))
- //Output: rrrphant
- //Here, there is ‘ele’ before ‘p’ and it is replaced by given ‘rrr’, so it becomes ‘rrrphant’
- //In Kotlin, replaceBeforeLast method replaces part of the string before the occurrence of last given character with the new given string.
- /**
- * replaceAfterLast method
- */
- var c2: String = "androidkotlin "
- println(c2.replaceBeforeLast('o', "yyyy"))
- //Output: andryyyyotlin
- //Here, there is ‘oidk’ before last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘yyyyotlin’
- /**
- * reversed method
- */
- var d1: String = "android"
- println(d1.reversed())
- //Output: diordna
- /**
- * startsWith method
- */
- var d2: String = "android"
- if (d2.startsWith("an")) {
- println("true")
- } else {
- println("false")
- }
- //Output: true
- /**
- * endsWith method
- */
- var d3: String = "android"
- if (d3.endsWith("id")) {
- println("true")
- } else {
- println("false")
- }
- //Output: true
- }
- }
Run the application and see the output as below in logcat.
Ranges
In Kotlin, there is range, that can define a set of variables. 1..5 means it defines set of variables from 1 to 5.
For example,
- /**
- * Ranges
- */
- var range = 1. .10 //this means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- for (i in range) {
- println(i)
- }
- //Output: 1 2 3 4 5 6 7 8 9 10
downTo
In Kotlin, downTo defines variables from higher to lower.
For example:
- /**
- * downTo
- */
- var a = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- for (i in a) {
- println(i)
- }
- //Output: 10 9 8 7 6 5 4 3 2 1
downTo with step
In Kotlin, downTo defines variables from higher to lower, but when a step is added in the prefix, then it will go through a specified interval.
For example:
- /**
- * downTo with step
- */
- var b = 10 downTo 1 step 2 //add range with interval 2. this means 10, 8, 6, 4, 2, 1
- for (i in b) {
- println(i)
- }
- //Output: 10 8 6 4 2 1
downTo() with reversed
In Kotlin, we can reverse the downTo using reversed() method,
For example:
- /**
- * downTo with reversed
- */
- var c = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- for (i in c.reversed()) {
- println(i)
- }
- //Output: 1 2 3 4 5 6 7 8 9 10
Complete Code Snippet for ranges
- package com.example.kotlin_helloworld
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- class MainActivity: AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle ? ) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- /**
- * Ranges
- */
- var range = 1. .10 //this means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- for (i in range) {
- println(i)
- }
- //Output: 1 2 3 4 5 6 7 8 9 10
- /**
- * downTo
- */
- var a = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- for (i in a) {
- println(i)
- }
- //Output: 10 9 8 7 6 5 4 3 2 1
- /**
- * downTo with step
- */
- var b = 10 downTo 1 step 2 //add range with interval 2. this means 10, 8, 6, 4, 2, 1
- for (i in b) {
- println(i)
- }
- //Output: 10 8 6 4 2 1
- /**
- * downTo with reversed
- */
- var c = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- for (i in c.reversed()) {
- println(i)
- }
- //Output: 1 2 3 4 5 6 7 8 9 10
- }
- }
Run the application and see the output as below in logcat.

Get the projects from here:
Thanks and happy coding.

Join the conversation! Your thoughts help the community grow.