JAVA 判断字符是否存在
时间: 2023-07-02 13:05:49 浏览: 41
可以使用 Java 中的 String 类的 contains() 方法来判断一个字符是否存在于另一个字符串中。contains() 方法返回一个 boolean 值,如果字符存在于字符串中,返回 true,否则返回 false。
示例代码如下:
```java
String str = "Hello World";
char ch = 'o';
if (str.contains(String.valueOf(ch))) {
System.out.println("字符 " + ch + " 存在于字符串中");
} else {
System.out.println("字符 " + ch + " 不存在于字符串中");
}
```
输出:
```
字符 o 存在于字符串中
```
另外,也可以使用 String 类的 indexOf() 方法来判断一个字符是否存在于另一个字符串中。如果字符存在于字符串中,indexOf() 方法返回该字符在字符串中的位置(从0开始),否则返回 -1。
示例代码如下:
```java
String str = "Hello World";
char ch = 'o';
if (str.indexOf(ch) != -1) {
System.out.println("字符 " + ch + " 存在于字符串中");
} else {
System.out.println("字符 " + ch + " 不存在于字符串中");
}
```
输出:
```
字符 o 存在于字符串中
```
相关问题
Java 判断路径是否存在
可以使用 Java 的 File 类的 exists() 方法来判断路径是否存在。
示例代码:
```java
import java.io.File;
public class PathExist {
public static void main(String[] args) {
String path = "/path/to/file";
File file = new File(path);
if (file.exists()) {
System.out.println("路径存在");
} else {
System.out.println("路径不存在");
}
}
}
```
在上述代码中,我们首先定义了一个路径字符串,然后创建一个 File 对象,将路径字符串传递给构造函数。接着,我们调用 exists() 方法来判断路径是否存在,如果存在,则输出“路径存在”,否则输出“路径不存在”。
java判断数据是否存在语法
Java判断数据是否存在通常有两种方法:
1. 使用if语句:
如果你想要判断一个数组中是否存在某个元素,可以使用以下代码:
```
int[] arr = {1, 2, 3, 4, 5};
int searchNum = 3;
boolean found = false;
for(int i = 0; i < arr.length; i++){
if(arr[i] == searchNum){
found = true;
break;
}
}
if(found){
System.out.println("Number " + searchNum + " is found in the array.");
}else{
System.out.println("Number " + searchNum + " is not found in the array.");
}
```
在这段代码中,我们使用了一个布尔类型的变量found来表示是否找到了要查找的元素。
如果你想要判断一个字符串中是否存在某个子串,可以使用以下代码:
```
String str = "This is a text.";
String searchStr = "text";
boolean found = str.contains(searchStr);
if(found){
System.out.println("Substring " + searchStr + " is found in the string.");
}else{
System.out.println("Substring " + searchStr + " is not found in the string.");
}
```
在这段代码中,我们使用了String类中的contains方法来判断一个字符串中是否包含了某个子串。
2. 使用Collections类中的contains方法:
如果你想要判断一个List中是否存在某个元素,可以使用以下代码:
```
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
if(list.contains(3)){
System.out.println("Number 3 is found in the list.");
}else{
System.out.println("Number 3 is not found in the list.");
}
```
在这段代码中,我们使用了List中的contains方法来判断一个集合中是否包含了某个元素。如果集合中包含了元素3,那么contains方法将会返回true。
相关推荐
![](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)
![](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)