相当于 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