Java 字符串包含
时间: 2023-08-15 07:13:31 浏览: 104
Java中判断一个字符串是否包含另一个字符串,可以使用String类的contains()方法。这个方法返回一个布尔值,表示被检查的字符串是否包含指定的字符串。以下是使用contains()方法的示例代码:
```java
String str1 = "Hello, world!";
String str2 = "world";
if (str1.contains(str2)) {
System.out.println("str1包含str2");
} else {
System.out.println("str1不包含str2");
}
```
在上面的代码中,我们判断了字符串`str1`是否包含字符串`str2`。如果包含,则输出"str1包含str2",否则输出"str1不包含str2"。
需要注意的是,contains()方法是区分大小写的。如果需要不区分大小写地判断字符串是否包含,可以使用String类的toLowerCase()方法或toUpperCase()方法将两个字符串都转换为小写或大写字母,然后再进行比较。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)