In this article, we are going to learn about Strings and Ranges in Kotlin with string methods.
Strings
Kotlin 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:
-
-
-
- var name: String = "Kishor"
- if (name.equals("kishor")) {
- println("They are equal")
- } else {
- println("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")
- }
-
- toInt() method
In Kotlin, we can convert String into integer using toInt() method.
For example:
-
-
-
- var a: String = "4"
- print(a.toInt());
-
- substring() method
We can get the substring of String. We can print the first name using substring method. For example:
-
-
-
- var myName: String = "Kishor Bikram Oli"
- println(myName.substring(0..5))
-
-
- String templates
In Java, we can print the name like below:-
-
-
- var name: String = "Kishor"
- println("My name is " + name)
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:
-
-
-
- var a1: String = "Hello"
- var a2: String = "World"
- if (a1.contains('e')) {
- println("true")
- } else {
- println("false")
- }
-
- replace() method
In Kotlin, replace() method replaces the old char with the new char.For example:
-
-
-
- var b1: String = "apple"
- println(b1.replace("pp", "tt"))
-
- replaceAfter() method
In Kotlin, replaceAfter() method replaces part of the string after the given character with the new given string.
For example:
-
-
-
- var b2: String = "elephant"
- println(b2.replaceAfter('p', "rr"))
-
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:
-
-
-
- var b3: String = "androidkotlin "
- println(b3.replaceAfter('o', "yyyy"))
-
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:
-
-
-
- var b2: String = "elephant"
- println(b2.replaceBefore('p', "rrr"))
-
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:
-
-
-
- var b2: String = "androidkotlin "
- println(b2.replaceBeforeLast('o', "yyyy"))
-
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:
-
-
-
- var d1: String "android"
- println(d1.reversed())
-
- startsWith() method
In Kotlin, startsWith() method returns true if the string starts with the given prefix.
For example:
-
-
-
- var d2: String = "android"
- if (d2.startsWith("an")) {
- println("true")
- } else {
- println("false")
- }
-
- endsWith() method
In Kotlin, endsWith() method returns true if the string ends with the given prefix.
For example:
-
-
-
- var d3: String = "android"
- if (d3.startsWith("id")) {
- println("true")
- } else {
- println("false")
- }
-
Complete code snippet for Strings
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,
-
-
-
- var range = 1. .10
- for (i in range) {
- println(i)
- }
-
downTo
In Kotlin, downTo defines variables from higher to lower.
For example:
-
-
-
- var a = 10 downTo 1
- for (i in a) {
- println(i)
- }
-
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:
-
-
-
- var b = 10 downTo 1 step 2
- for (i in b) {
- println(i)
- }
-
downTo() with reversed
In Kotlin, we can reverse the downTo using reversed() method,
For example:
-
-
-
- var c = 10 downTo 1
- for (i in c.reversed()) {
- println(i)
- }
-
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)
-
-
-
- var range = 1. .10
- for (i in range) {
- println(i)
- }
-
-
-
-
- var a = 10 downTo 1
- for (i in a) {
- println(i)
- }
-
-
-
-
- var b = 10 downTo 1 step 2
- for (i in b) {
- println(i)
- }
-
-
-
-
- var c = 10 downTo 1
- for (i in c.reversed()) {
- println(i)
- }
-
- }
- }
Run the application and see the output as below in logcat.
Get the projects from here:
Thanks and happy coding.