我有一个包含几个搜索词的输入字符串,用于在包含所有搜索词的文本中查找一行。
例如:
String searchTerms = "java stackoverflow conditions";
String [] splittedTerm = searchTerms.split(" ");
搜索词是 AND 连接词:
if (textLine.contains(splittedTerm[0] && textLine.contains(splittedTerm[1]) && textLine.contains(splittedTerm[2])) start=true;
但是搜索词的数量是动态的,它总是取决于用户的请求......
那么是否有可能根据搜索词的数量使用 if 语句?
最佳答案
您需要循环遍历分割字符串后得到的String[]
:-
首先将要比较的所有元素添加到数组中,然后迭代并比较第一个数组和 split() 返回的数组。确保两个数组的长度相等
boolean flag=true;
String searchTerms = "java stackoverflow conditions hello test";
String [] splittedTerm = searchTerms.split(" ");
for(int i=0;i<splittedTerm.length;i++){
if (!(textLine[i].equals(splittedTerm[i]))){ //textLine is the array containing String literals you want to compare.
flag=false;
}
}
start=flag;
关于java - if 语句中的动态 AND 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24906523/