How to compare strings kotlin

How to Compare Strings in Kotlin?

In this tutorial, you shall learn how to compare given two strings in Kotlin, using String.compareTo() function, with examples.

Kotlin – Compare Strings

To compare strings in Kotlin, use String.compareTo() method.

Given two strings str1 and str2 , and if we would like to compare string str1 to string str2 , call compareTo() method on string str1 and pass the string str2 as argument to the method as shown below.

  • zero, if str1 equals str2
  • negative number if str1 is less than str2 lexicographically
  • positive number if str1 is greater than str2 lexicographically

Examples

1. Two strings are equal

In this example, we will take two strings in str1 and str2, such that both have same string values. We shall compare them using compareTo() method and observe the result.

Kotlin Program

Value returned by compareTo() : 0 The two strings are equal.

2. First string is less than the second string

In this example, we will take two strings in str1 and str2, such that str1 occurs before str2 in lexicographical order. We shall compare them using compareTo() method and observe the result. compareTo() should return a negative value.

Kotlin Program

Value returned by compareTo() : -1 This string str1 is less than the other string str2.

3. First string is greater than the second string

In this example, we will take two strings in str1 and str2, such that str1 occurs after str2 in lexicographical order. We shall compare them using compareTo() method and observe the result. compareTo() should return a positive value.

Читайте также:  Google apps script parse html

Kotlin Program

Value returned by compareTo() : 1 This string str1 is greater than the other string str2.

Conclusion

In this Kotlin Tutorial, we learned how to compare two strings using String.compareTo() method, with the help of Kotlin example programs.

  • How to Check if String Ends with Specified Character in Kotlin?
  • How to Check if String Ends with Specified String in Kotlin?
  • How to Check if String Starts with Specified Character in Kotlin?
  • How to Check if String Starts with Specified String Value in Kotlin?
  • How to Check if Two Strings are Equal in Kotlin?
  • How to Compare Strings in Kotlin?
  • How to Create an Empty String in Kotlin?
  • How to Filter Characters of String in Kotlin?
  • How to Filter List of Strings based on Length in Kotlin?
  • How to Filter only Non-Empty Strings of Kotlin List?
  • How to Filter only Strings from a Kotlin List?
  • How to Get Character at Specific Index of String in Kotlin?
  • How to Initialize String in Kotlin?
  • How to Iterate over Each Character in the String in Kotlin?
  • How to Remove First N Characters from String in Kotlin?
  • How to Remove Last N Characters from String in Kotlin?
  • How to check if a String contains Specified String in Kotlin?
  • How to create a Set of Strings in Kotlin?
  • How to define a List of Strings in Kotlin?
  • How to define a String Constant in Kotlin?
  • How to get Substring of a String in Kotlin?
  • Kotlin String Concatenation
  • Kotlin String Length
  • Kotlin String Operations
  • Kotlin String.capitalize() – Capitalize First Character
  • Kotlin – Split String to Lines – String.lines() function
  • Kotlin – Split String – Examples
  • Kotlin – String Replace – Examples
  • Kotlin – String to Integer – String.toInt()
  • [Solved] Kotlin Error: Null can not be a value of a non-null type String

Источник

How to compare two strings in Kotlin

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

The Kotlin language allows multiple ways to check whether or not two strings are equal. Some of them are:

1. Using comparision operators

In Kotlin, == is used to check the structural equality of two objects. It will return true if both the objects have the same value:

fun main(args : Array)
val str1: String = "Hello"
val str2: String = "Hello World"
val str3: String = "Hello"
println(str1 == str2) // returns false
println(str1 == str3) // returns true
>

Koltin uses the === operator for referential equality. It returns true if the two variables are pointing to the same object and have the same value.

Whenever we initialize a new String object using «» , it is automatically placed in the string pool. Such strings will always reference the same object.

fun main(args : Array)
val str1: String = "Hello"
val str2 = String("Hello".toCharArray())
println(str1 === str2) // returns false
println(str1 == str2) // returns true
>

2. Using equals() function

Another method to compare two strings in Kotlin is to use the equals() function.

This comparison is case sensitive. For case-insensitive string comparison in Kotlin, pass the second argument as True .

fun main(args : Array)
val str1: String = "Hello"
val str2: String = "Hello World"
val str3: String = "Hello"
val str4: String = "hello"
println(str1.equals(str2)) // returns false
println(str1.equals(str3)) // returns true
println(str1.equals(str4, true)) // returns true
>

3. Using CompareTo() function

Kotlin has another method, CompareTo() , which is used to check the order of two strings.

CompareTo() returns Int value instead of Boolean , as follows:

  1. If two strings are equal, it returns 0
  2. If the string is less than the other string, it returns a negative number
  3. If the string is greater than the other string, it returns a positive number

This comparison is case sensitive. For case-insensitive string comparison in Kotlin, pass the second argument as True .

Источник

Оцените статью