Java compareToIgnoreCase() 方法

Java 教程 · 2019-02-08 19:42:11

compareToIgnoreCase() 方法用于按字典顺序比较两个字符串,不考虑大小写。

语法

int compareToIgnoreCase(String str)

参数

  • str -- 要比较的字符串。

返回值

  • 如果参数字符串等于此字符串,则返回值 0;
  • 如果此字符串小于字符串参数,则返回一个小于 0 的值;
  • 如果此字符串大于字符串参数,则返回一个大于 0 的值。

实例

public class Test {

    public static void main(String args[]) {
        String str1 = "STRINGS";
        String str2 = "Strings";
        String str3 = "Strings123";

        int result = str1.compareToIgnoreCase( str2 );
        System.out.println(result);
      
        result = str2.compareToIgnoreCase( str3 );
        System.out.println(result);
     
        result = str3.compareToIgnoreCase( str1 );
        System.out.println(result); 
    }
}

以上程序执行结果为:

0
-3
3

点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html

查看所有标签

Structure and Interpretation of Computer Programs - 2nd Edition

Structure and Interpretation of Computer Programs - 2nd Edition

Harold Abelson、Gerald Jay Sussman / The MIT Press / 1996-7-25 / USD 145.56

Structure and Interpretation of Computer Programs has had a dramatic impact on computer science curricula over the past decade. This long-awaited revision contains changes throughout the text. Ther......一起来看看 《Structure and Interpretation of Computer Programs - 2nd Edition 》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具