Java中如何校验一个string类型的集合是否有包含-Axls的数据
可以使用Java中的字符串方法contains()来判断一个字符串是否包含"-A.xls"。具体的步骤如下:
- 遍历字符串集合,可以使用for-each循环或者Stream流。
- 对于每个字符串,使用contains()方法判断是否包含"-A.xls"。
- 如果找到包含"-A.xls"的字符串,则返回true,表示集合中有包含该数据。如果遍历完整个集合仍然没有找到包含的字符串,则返回false。
下面是一个示例代码:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("file1.xlsx");
stringList.add("file2-A.xls");
stringList.add("file3.xls");
boolean containsFile = containsFile(stringList);
System.out.println("集合中是否包含'-A.xls'的数据:" + containsFile);
}
public static boolean containsFile(List<String> stringList) {
for (String s : stringList) {
if (s.contains("-A.xls")) {
return true;
}
}
return false;
}
}
输出结果为:
集合中是否包含'-A.xls'的数据:true
注意:这个方法只会判断集合中是否有包含"-A.xls"的字符串,不会返回具体是哪个字符串包含了该数据。如果需要返回具体的字符串,可以将符合条件的字符串添加到另一个集合中,或者使用Java 8中的Stream流过滤出符合条件的字符串
原文地址: https://cveoy.top/t/topic/iHvl 著作权归作者所有。请勿转载和采集!