Kotlin String - compareTo() Function - The Kotlin string compareTo() function is used to compare two strings lexicographically. It determines the ordering of two strings and returns zero if current object is equal to the specified other object, a negativ
Comparing Strings ThecompareTo(string)function compares two strings and returns 0 if both are equal: Example vartxt1="Hello World" vartxt2="Hello World"println(txt1.compareTo(txt2))// Outputs 0 (they are equal) Try it Yourself » ...
println("Strings are not equal") } } In the example, we compare two strings. if (s1 == s2) { The==operator compares structural equality, that is, the content of the two strings. val res = s1.compareTo(s2, true) ThecompareTomethod compares two strings lexicographically, optionally igno...
Kotlin also has acompareTomethod which we can use to compare the order of the two strings. Similarly, as theequalsmethod, thecompareTomethod also comes with an optionalignoreCaseargument: assertTrue { first.compareTo(second) ==0} assertTrue { first.compareTo(firstCapitalized) ==32} assertTrue...
How to compare two strings in Kotlin? We can compare string using the below operators - 1.Using “==” operator: 2.compareTo() extension function Can you migrate code from java to Kotlin? Yes! Off course, the JetBrains IDEA provides inbuilt tool....
3.8.compareTo() Another interesting function is thecompareTo()function. It is used to compare two strings and accepts one string parameterotheras seen in the function definition: fun compareTo(other: String): Int String.compareTo()returns anintvalue: ...
Kotlin – Check if Two Strings are Equal Kotlin – Check if String contains Specified Substring Kotlin – Check if String Starts with Specified Character Kotlin – Check if String Ends with Specified String Kotlin – Compare Strings Kotlin – Filter Characters of String ...
If you compare this declaration to the array declaration earlier in the guide, you see that the declarations are almost the same. The only major difference is that you use listOf(), rather than arrayOf(). All of the same coding examples, like the one shown below, work well. for (i ...
让我们举一个例子来比较 Kotlin 中的两个字符串。有两种比较字符串的方法,使用equals()方法或使用compareTo()方法。 /** * created by Chaitanya for Beginnersbook.com */packagebeginnersbookfunmain(args :Array<String>){varstr1 ="BeginnersBook"varstr2 ="beginnersbook"/** ...
The expression “if” will return a value whenever necessary. Like other programming language, “if-else” block is used as an initial conditional checking operator. In the following example, we will compare two variables and provide the required output accordingly....