java中怎么判断一个字符串数组中包含某个字符或字符串

如题所述

1:描述

java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列

2:声明

public boolean contains(CharSequence s)

3:返回值

此方法返回true,如果此字符串包含,否则返回false。

4:实例

public static void main(String[] args) 

{String str = "abc";

boolean status = str.contains("a");

if(status){System.out.println("包含");}

else{System.out.println("不包含");}}

扩展资料

字符串或串(String)是由数字、字母、下划线组成的一串字符。它是编程语言中表示文本的数据类型。在程序设计中,字符串为符号或数值的一个连续序列。字符串操作就是以串的整体作为操作对象,如:在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。

对于字符串的操作方法,在这里通过介绍C语言、C++和java这三种常用的语言来说明。

参考资料

百度百科-字符串操作

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-16

字符串有一个contains方法。

如下:若要查询字符串数组中是否有“dog"。

如果想查询字符,只需要将字符串,替换为:字符+“”就可以了。

第2个回答  2017-06-07
String[] colors = {"blue","red","green","yellow","orange","black"};
int index=Arrays.binarySearch(colors, "orange");
如果存在,index为数组下标,不存在-1。
第3个回答  2017-05-17
String[] a = ...;
String target = "aa";
boolean flag = false;
for(int i = 0; i < a.length; i++){
    if(a[i].contains(target)){
        flag = true;
    }
}
if(flag){
    System.out.println("存在");
} else {
    System.out.println("不存在");
}

大概的思路是这样子,编码细节就自己写吧

第4个回答  2017-05-17

public static void main(String[] args) {

String string = "被检测的字符串";

String[] arr = {};//字符串数组

boolean tag = true;

for (String str : arr) {

if (str.equals(string)) {

System.out.println("存在");

tag = false;

break;

}

}

if (tag) {

System.out.println("不存在");

}

}


相似回答