java 字符串对比
时间: 2024-01-29 20:07:35 浏览: 34
Java 中字符串的对比可以使用 `equals()` 方法或 `==` 运算符。其中,`equals()` 方法比较的是字符串的内容是否相同,而 `==` 运算符比较的是两个字符串对象的引用是否相同。
例如:
```java
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
System.out.println(str1.equals(str2)); // true
System.out.println(str1 == str2); // true
System.out.println(str1.equals(str3)); // true
System.out.println(str1 == str3); // false
```
在上面的例子中,`str1` 和 `str2` 都是使用字符串常量创建的,它们的值相同,因此 `equals()` 方法和 `==` 运算符都返回 `true`。而 `str3` 是使用 `new` 关键字创建的新的字符串对象,虽然它的值也是 "hello",但它和 `str1`、`str2` 的引用不同,因此 `==` 运算符返回 `false`。
相关问题
java 字符串比较
Java中字符串比较可以使用多种方法,其中最常用的是equals()方法。该方法比较两个字符串的内容是否相同,而不是比较它们的引用是否相同。因此,即使两个字符串对象的引用不同,只要它们的内容相同,equals()方法就会返回true。
以下是Java中字符串比较的三种方法:
1.使用equals()方法比较字符串内容是否相同。
```java
String str1 = "hello";
String str2 = "world";
if(str1.equals(str2)){
System.out.println("str1和str2的内容相同");}else{
System.out.println("str1和str2的内容不同");
}
```
2.使用compareTo()方法比较字符串的字典顺序。该方法返回一个整数,如果字符串在字典中排在参数字符串之前,则返回负数;如果字符串在字典中排在参数字符串之后,则返回正数;如果两个字符串相等,则返回0。
```java
String str1 = "hello";
String str2 = "world";
int result = str1.compareTo(str2);
if(result < 0){
System.out.println("str1在字典中排在str2之前");
}else if(result > 0){
System.out.println("str1在字典中排在str2之后");
}else{
System.out.println("str1和str2在字典中的位置相同");
}
```
3.使用==运算符比较两个字符串的引用是否相同。如果两个字符串的引用指向同一个对象,则返回true;否则返回false。
```java
String str1 = "hello";
String str2 = "hello";
if(str1 == str2){
System.out.println("str1和str2的引用相同");
}else{
System.out.println("str1和str2的引用不同");
}
```
java字符串对比函数
Java中字符串对比函数有两种:equals()和compareTo()。
1. equals()函数:用于比较两个字符串的内容是否相同,区分大小写。
示例代码:
```
String str1 = "hello";
String str2 = "HELLO";
boolean result = str1.equals(str2);
System.out.println(result); // false
```
2. compareTo()函数:用于比较两个字符串的字典顺序,返回值为整数类型,如果返回值为0,则表示两个字符串相等;如果返回值为正数,则表示当前字符串大于目标字符串;如果返回值为负数,则表示当前字符串小于目标字符串。
示例代码:
```
String str1 = "abc";
String str2 = "def";
int result = str1.compareTo(str2);
System.out.println(result); // -3
```
注意:compareTo()函数区分大小写,如果需要不区分大小写,则可以使用compareToIgnoreCase()函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)