相當於 Java 的 equalsIgnoreCase 的 Kotlin
Java 提供了一種名為 equalsIgnoreCase() 的 String 方法,幫助開發者根據字串的內容比較兩條字串。這種比較是大小寫不敏感的,也就是說,它忽略字串是大寫還是小寫,而只是比較字串值。在本文中,我們將探討如何在 Kotlin 中實現相同的功能。
示例 – Java 中的 equalsIgnoreCase
以下示例演示了 equalsIgnoreCase() 在 Java 中的工作原理。
public class MyClass {
public static void main(String args[]){
String s1="TutorialsPoint";
String s2="tutorialspoint";
System.out.println("String 1: " + s1);
System.out.println("String 2: " + s2);
// Strings match as we ignore the case
System.out.println("Strings match? : " + s1.equalsIgnoreCase(s2));
}
}輸出
它將產生以下輸出 −
String 1: TutorialsPoint String 2: tutorialspoint Strings match? : true
示例 – Kotlin 中的 equalsIgnoreCase
現在,我們來看看如何在 Kotlin 中實現相同概念。
fun main(args: Array<String>) {
val t1 = "TutorialsPoint.com";
val t2 = "TutorialsPoint";
val t3 = "tutorialspoint";
// false as the strings do not match
println("String 1: " + t1)
println("String 2: " + t2)
println("String 3: " + t3)
// comparing t1 and t2
println("Strings 1 and 2 match? : " + t1.equals(t2, ignoreCase = true));
// comparing t2 and t3
// both the strings match as we ignore their case
println("Strings 2 and 3 match? : " + t2.equals(t3, ignoreCase = true));
}輸出
它將產生以下輸出 −
String 1: TutorialsPoint.com String 2: TutorialsPoint String 3: tutorialspoint Strings 1 and 2 match? : false Strings 2 and 3 match? : true
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP