Java 中忽略大写和小写
本篇文章介绍如何在 Java 中忽略字符串的大小写。
字符串是一个字符序列,在 Java 中,它是一个类,还包含多个实用方法。字符串可以是小写或大写或两者的混合,这很好,但有时在比较字符串时会产生问题,因为每个字符都有唯一的 Unicode 值。
在本文中,我们将学习在忽略大小写的情况下处理字符串。我们熟悉使用 equals()
方法比较两个字符串。当两个字符串相等时,此方法返回 true
,包括它们的大小写。
例如,看下面的例子。
import java.util.*;
import java.lang.Object;
public class SimpleTesting {
public static void main(String args[]) {
String a = "SimpleTesting";
String b = "SimpleTesting";
String c = "SimpleTesting";
a.equals(b); // return false
a.equals(c); // returns true
}
}
这让我们想知道是否有任何方法可以在忽略它们的情况下比较两个字符串。
那么,Java 为我们提供了各种方法来完成这项任务。在这里,我们将详细讨论这些方法:
-
使用
toUppercase()
方法忽略大小写 -
使用
toLowerCase()
方法忽略大小写 -
使用
equalsIgnoreCase()
方法忽略大小写 -
使用
compareToIgnoreCase()
方法忽略大小写
在 Java 中使用 toUppercase()
方法忽略大小写
在这种方法中,我们首先将两个字符串都转换为大写,然后调用 equals()
方法。此方法的签名是:
public String toUpperCase()
此方法根据默认语言环境将所有字符串字符转换为大写。这意味着只有在默认语言环境为 Locale.ENGLISH
或 Locale.US
时,icecream
才会转换为 ICECREAM
。
如果默认本地设置为无法识别这些字符的其他国家或语言,我们可能会得到不可预知的结果。我们可以将语言环境传递给函数调用来避免这种情况。
现在让我们使用 Java 中的 toUpperCase()
和 equals()
方法比较两个字符串。看下面的代码:
import java.util.*;
import java.lang.Object;
public class SimpleTesting {
public static void main(String args[]) {
String desert1 ="icecream";
String desert2 = "IceCream";
//converting to both the strings to upper case
String desert1_converted = desert1.toUpperCase();
String desert2_converted = desert2.toUpperCase();
//comparing both the desert
boolean same_or_not1 = desert1_converted.equals(desert2_converted);
boolean same_or_not2 = desert1.equals(desert2);
System.out.println("Comparison with conversion: " +same_or_not1);
System.out.println("Comparison without conversion: " +same_or_not2);
}
}
输出:
Comparison with conversion: true
Comparison without conversion: false
在上面的示例中,toUpperCase()
无需传递语言环境就可以正常工作,因为我们的默认语言环境是英语。
在 Java 中使用 toLowerCase()
方法忽略大小写
此方法与前一种方法一样,只是它将两个字符串的所有字符都转换为小写。方法签名是:
public String toLowerCase()
根据默认语言环境,此过程将字符串中的所有字符更改为小写。
这意味着如果默认语言环境是 Locale.ENGLISH
或 Locale.US
,那么 ICECREAM
将被转换为 icecream
。如果默认本地设置为无法识别这些字符的其他国家或语言,我们可能会得到不可预知的结果。
我们可以将语言环境作为参数传递给函数调用来避免这种情况。让我们仔细看看这个功能是如何运作的。
现在让我们使用 Java 中的 toLowerCase()
和 equals()
方法比较两个字符串。看下面的代码:
import java.util.*;
import java.lang.Object;
public class SimpleTesting {
public static void main(String args[]) {
String desert1 ="icecream";
String desert2 = "IceCream";
//converting to both the strings to lower case
String desert1_converted = desert1.toLowerCase();
String desert2_converted = desert2.toLowerCase();
//comparing both the desert
boolean same_or_not1 = desert1_converted.equals(desert2_converted);
boolean same_or_not2 = desert1.equals(desert2);
System.out.println("Comparison with conversion: " +same_or_not1);
System.out.println("Comparison without conversion: " +same_or_not2);
}
}
输出:
Comparison with conversion: true
Comparision without conversion: false
在 Java 中使用 equalsIgnoreCase()
方法忽略大小写
equalsIgnoreCase() 这个方法和 equals()
方法一样,只是它忽略了字符串的大小写。我们先来看看方法签名。
public boolean equalsIgnoreCase(String anotherString)
如果两个字符串在忽略大小写后相等,则此方法返回 true
。
如果两个字符串的长度相同,并且两个字符串中对应的字符相同,则认为它们相等,忽略大小写。根据这种方法,ICECREAM
和 icecream
是相同的;因此,将返回 true
。
查看下面的代码以了解它是如何工作的。
import java.util.*;
import java.lang.Object;
public class SimpleTesting {
public static void main(String args[]) {
String desert1 ="icecream";
String desert2 = "IceCream";
//comparing both the deserts
boolean same_or_not1 = desert1.equalsIgnoreCase(desert2);
System.out.println("Comparison : " +same_or_not1);
}
}
输出:
Comparison : true
在 Java 中使用 compareToIgnoreCase()
方法忽略大小写
compareToIgnoreCase()
方法按字典顺序比较两个字符串,不考虑大小写差异。此方法返回一个整数,该整数等于前两个不相等字符之间的差。
它的签名是:
public int compareToIgnoreCase(String str)
如果两个字符串相等,则返回 0
。看下面的示例代码:
import java.util.*;
import java.lang.Object;
public class SimpleTesting {
public static void main(String args[]) {
String desert1 ="icecream";
String desert2 = "IceCream";
//comparing both the deserts
int same_or_not1 = desert1.compareToIgnoreCase(desert2);
System.out.println("Comparision : " +same_or_not1);
}
}
输出:
Comparision : 0
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。