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.

  1. equals() method
    In Kotlin, to compare two strings, we use equals() methods.

    For example:

    1. /**
    2. * equals method
    3. */
    4. var name: String = "Kishor"
    5. if (name.equals("kishor")) {
    6. println("They are equal")
    7. } else {
    8. println("They are not equal")
    9. }
    10. //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:

    1. if (name.equals("kishor", true)) {
    2. println("They are equal")
    3. } else {
    4. println("They are not equal")
    5. }
    6. //Output: They are equal.
  2. toInt() method

    In Kotlin, we can convert String into integer using toInt() method.

    For example:

    1. /**
    2. * String to Integer
    3. */
    4. var a: String = "4"
    5. print(a.toInt());
    6. //Output: 4
  3. substring() method
    We can get the substring of String. We can print the first name using substring method. For example:
    1. /**
    2. *Substring
    3. */
    4. var myName: String = "Kishor Bikram Oli"
    5. println(myName.substring(0..5)) //this means 0 to 5
    6. //Output: Kishor
  4. String templates
    In Java, we can print the name like below:
    1. /**
    2. * String template
    3. */
    4. var name: String = "Kishor"
    5. println("My name is " + name) //Java

    But in Kotlin, we can use String using String templates like:

    println("My name is $name") //Kotlin

  5. 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:

    1. /**
    2. * Contains method
    3. */
    4. var a1: String = "Hello"
    5. var a2: String = "World"
    6. if (a1.contains('e')) {
    7. println("true")
    8. } else {
    9. println("false")
    10. }
    11. //Output: true
  6. replace() method
    In Kotlin, replace() method replaces the old char with the new char.

    For example:

    1. /**
    2. * replace method
    3. */
    4. var b1: String = "apple"
    5. println(b1.replace("pp", "tt"))
    6. //Output: attle
  7. replaceAfter() method

    In Kotlin, replaceAfter() method replaces part of the string after the given character with the new given string.

    For example:

    1. /**
    2. * replaceAfter method
    3. */
    4. var b2: String = "elephant"
    5. println(b2.replaceAfter('p', "rr"))
    6. //Output: eleprr

    Here, there is ‘hant’ after ‘p’ and it is replaced by ‘rr’, so it becomes ‘eleprr’

  8. 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:

    1. /**
    2. * replaceAfterLast method
    3. */
    4. var b3: String = "androidkotlin "
    5. println(b3.replaceAfter('o', "yyyy"))
    6. //Output: androidkoyyyy

    Here, there is ‘tlin after last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘androidkoyyyy’

  9. replaceBefore() method
    In Kotlin, replaceBefore() method replaces part of the string before the given character with the new given string.

    For example:

    1. /**
    2. * replaceBefore method
    3. */
    4. var b2: String = "elephant"
    5. println(b2.replaceBefore('p', "rrr"))
    6. //Output: rrrphant

    Here, there is ‘ele’ before ‘p’ and it is replaced by given ‘rrr’, so it becomes ‘rrrphant’

  10. 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:

    1. /**
    2. * replaceAfterLast method
    3. */
    4. var b2: String = "androidkotlin "
    5. println(b2.replaceBeforeLast('o', "yyyy"))
    6. //Output: yyyyotlin

    Here, there is ‘androidk’ before last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘yyyyotlin’

  11. reversed() method
    In Kotlin, reversed() method gives the string in reversed format.

    For example:

    1. /**
    2. * reversed method
    3. */
    4. var d1: String "android"
    5. println(d1.reversed())
    6. //Output: diordna
  12. startsWith() method
    In Kotlin, startsWith() method returns true if the string starts with the given prefix.

    For example:

    1. /**
    2. * startsWith method
    3. */
    4. var d2: String = "android"
    5. if (d2.startsWith("an")) {
    6. println("true")
    7. } else {
    8. println("false")
    9. }
    10. //Output: true
  13. endsWith() method
    In Kotlin, endsWith() method returns true if the string ends with the given prefix.

    For example:

    1. /**
    2. * endsWith method
    3. */
    4. var d3: String = "android"
    5. if (d3.startsWith("id")) {
    6. println("true")
    7. } else {
    8. println("false")
    9. }
    10. //Output: true

Complete code snippet for Strings

  1. package com.example.kotlin_helloworld
  2. import android.support.v7.app.AppCompatActivity
  3. import android.os.Bundle
  4. class MainActivity: AppCompatActivity() {
  5. override fun onCreate(savedInstanceState: Bundle ? ) {
  6. super.onCreate(savedInstanceState)
  7. setContentView(R.layout.activity_main)
  8. /**
  9. * Strings
  10. */
  11. var name: String = "Kishor"
  12. /**
  13. * String template
  14. */
  15. if (name.equals("kishor")) {
  16. println("They are equal")
  17. } else {
  18. println("They are not equal")
  19. }
  20. //Output: They are not equal. Since it is case sensitive.
  21. //If you want to compare without taking case sensitive into consideration then we can pass
  22. // "true" as parameter
  23. if (name.equals("kishor", true)) {
  24. println("They are equal")
  25. } else {
  26. println("They are not equal")
  27. }
  28. //Output: They are equal. Since it is not case sensitive this time.
  29. /**
  30. * String to Integer
  31. */
  32. //We can convert String into integer
  33. var a: String = "4"
  34. print(a.toInt());
  35. //Output: 4
  36. /**
  37. *Substring
  38. */
  39. //We can get the substring of string
  40. var myName: String = "Kishor Bikram Oli"
  41. //We can print the first name using substring method
  42. println(myName.substring(0. .5)) //starting from zero index to index 5
  43. //Output: Kishor
  44. //String template
  45. //In Java, we can print name like below:
  46. println("My name is " + name) //Java
  47. //But in Kotlin, we can use String template like:
  48. println("My name is $name") //Kotlin
  49. //Contains
  50. //In Kotlin, if we want to check if some character is in the string, then we can use contains methods like below.
  51. /**
  52. * Contains method
  53. */
  54. var a1: String = "Hello"
  55. var a2: String = "World"
  56. if (a1.contains('l')) {
  57. println("true")
  58. } else {
  59. println("false")
  60. }
  61. //Replace
  62. //In Kotlin, replace method replaces the old char with the new char like below:
  63. /**
  64. * replace method
  65. */
  66. var b1: String = "apple"
  67. println(b1.replace("pp", "tt"))
  68. //Output: attle
  69. //ReplaceAfter
  70. //In Kotlin, replaceAfter method replaces part of the string after the given character with the new given string.
  71. /**
  72. * replaceAfter method
  73. */
  74. var b2: String = "elephant"
  75. println(b2.replaceAfter('p', "rr", b2))
  76. //Output: eleprr
  77. //Here, there is ‘hant’ after ‘p’ and it is replaced by ‘rr’, so it becomes ‘eleprr’
  78. /**
  79. * replaceAfterLast method
  80. */
  81. // In Kotlin, replaceAfterLast method replaces part of the string after the occurrence of last given character with the new given string.
  82. var b3: String = "androidkotlin "
  83. println(b3.replaceAfterLast('o', "yyyy"))
  84. //Output: androidkoyyyy
  85. //Here, there is ‘tlin after last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘androidkoyyyy’
  86. //In Kotlin, replaceBefore method replaces part of the string before the given character with the new given string.
  87. /**
  88. * replaceBefore method
  89. */
  90. var c1: String = "elephant"
  91. println(c1.replaceBefore('p', "rrr"))
  92. //Output: rrrphant
  93. //Here, there is ‘ele’ before ‘p’ and it is replaced by given ‘rrr’, so it becomes ‘rrrphant’
  94. //In Kotlin, replaceBeforeLast method replaces part of the string before the occurrence of last given character with the new given string.
  95. /**
  96. * replaceAfterLast method
  97. */
  98. var c2: String = "androidkotlin "
  99. println(c2.replaceBeforeLast('o', "yyyy"))
  100. //Output: andryyyyotlin
  101. //Here, there is ‘oidk’ before last ‘o’ and it is replaced by ‘yyyy’, so it becomes ‘yyyyotlin’
  102. /**
  103. * reversed method
  104. */
  105. var d1: String = "android"
  106. println(d1.reversed())
  107. //Output: diordna
  108. /**
  109. * startsWith method
  110. */
  111. var d2: String = "android"
  112. if (d2.startsWith("an")) {
  113. println("true")
  114. } else {
  115. println("false")
  116. }
  117. //Output: true
  118. /**
  119. * endsWith method
  120. */
  121. var d3: String = "android"
  122. if (d3.endsWith("id")) {
  123. println("true")
  124. } else {
  125. println("false")
  126. }
  127. //Output: true
  128. }
  129. }

Run the application and see the output as below in logcat.

Android

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,

  1. /**
  2. * Ranges
  3. */
  4. var range = 1. .10 //this means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  5. for (i in range) {
  6. println(i)
  7. }
  8. //Output: 1 2 3 4 5 6 7 8 9 10

downTo

In Kotlin, downTo defines variables from higher to lower.

For example:

  1. /**
  2. * downTo
  3. */
  4. var a = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  5. for (i in a) {
  6. println(i)
  7. }
  8. //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:

  1. /**
  2. * downTo with step
  3. */
  4. var b = 10 downTo 1 step 2 //add range with interval 2. this means 10, 8, 6, 4, 2, 1
  5. for (i in b) {
  6. println(i)
  7. }
  8. //Output: 10 8 6 4 2 1

downTo() with reversed

In Kotlin, we can reverse the downTo using reversed() method,

For example:

  1. /**
  2. * downTo with reversed
  3. */
  4. var c = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  5. for (i in c.reversed()) {
  6. println(i)
  7. }
  8. //Output: 1 2 3 4 5 6 7 8 9 10

Complete Code Snippet for ranges

  1. package com.example.kotlin_helloworld
  2. import android.support.v7.app.AppCompatActivity
  3. import android.os.Bundle
  4. class MainActivity: AppCompatActivity() {
  5. override fun onCreate(savedInstanceState: Bundle ? ) {
  6. super.onCreate(savedInstanceState)
  7. setContentView(R.layout.activity_main)
  8. /**
  9. * Ranges
  10. */
  11. var range = 1. .10 //this means 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  12. for (i in range) {
  13. println(i)
  14. }
  15. //Output: 1 2 3 4 5 6 7 8 9 10
  16. /**
  17. * downTo
  18. */
  19. var a = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  20. for (i in a) {
  21. println(i)
  22. }
  23. //Output: 10 9 8 7 6 5 4 3 2 1
  24. /**
  25. * downTo with step
  26. */
  27. var b = 10 downTo 1 step 2 //add range with interval 2. this means 10, 8, 6, 4, 2, 1
  28. for (i in b) {
  29. println(i)
  30. }
  31. //Output: 10 8 6 4 2 1
  32. /**
  33. * downTo with reversed
  34. */
  35. var c = 10 downTo 1 //this means 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  36. for (i in c.reversed()) {
  37. println(i)
  38. }
  39. //Output: 1 2 3 4 5 6 7 8 9 10
  40. }
  41. }

Run the application and see the output as below in logcat.

Android

Get the projects from here:

Thanks and happy coding.