如何使我的字符串比较不区分大小写? [英] How do I make my string comparison case insensitive?

查看:116
本文介绍了如何使我的字符串比较不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Java程序来比较两个字符串:

I created one Java program to compare two strings:

String s1 = "Hello";
String s2 = "hello";

if (s1.equals(s2)) {
    System.out.println("hai");
} else {
    System.out.println("welcome");
}

显示welcome。我理解它是大小写敏感的。但我的问题是,我想比较两个字符串没有大小写敏感性,我预计输出是hai。

It displays "welcome". I understand it is case sensitive. But My problem is that I want to compare two strings without case sensitivity I.e I expect the output to be "hai".

推荐答案


  • 最好使用 s1.equalsIgnoreCase(s2):(参见 javadoc

  • 您也可以将它们转换为大写/小写并使用 s1.equals(s2)

    • The best would be using s1.equalsIgnoreCase(s2): (see javadoc)
    • You can also convert them both to upper/lower case and use s1.equals(s2)
    • 这篇关于如何使我的字符串比较不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆