contains是找指定字符串是否包含一个字串,返回值的boolean类型,即只有true和false。
indexOf有多个重载,但无论哪个,都是做一定的匹配,然后把匹配的第一个字符的位置返回,返回的是int类型,如果没找到,那么返回-1。
Excel中可以利用find函数判定某个字符串里面是否包含某个字符串。
软件版本:Office2007。
方法如下:
1.判断A列字符串中是否包含B列中的字符串:
2.输入公式如下:
3.下拉填充得到结果如下:
content()方法,判断字符串包含,或者用indexOf()方法,返回包含字符串第一次出现的索引位置,如果没找到返回-1:例如if(str.content(str1)){}或者if(str.indexOf(str1)=-1){}。
方案1 使用 commons-lang3 org.apache.commons.lang3.StringUtils.containsAny(CharSequence, char...) 方法 。
示例:
public static void main(String[] args){ //看看这个字符串 里面 有没有t String str = "feitianbenyue"; char t = 't'; System.out.println(StringUtils.containsAny(str, t)); }。
输出:
true
方案2: 如果你不是太在乎 查询是字符还是字符串的话, 可以使用 。
JDK API java.lang.String.indexOf(String)。
示例:
public static void main(String[] args){ //看看这个字符串 里面 有没有t String str = "feitianbenyue"; String t = "t"; //System.out.println(StringUtils.containsAny(str, t)); System.out.println(-1 != str.indexOf(t)); }。
输出:
true
方法:
使用String类的indexOf()方法可以判断一个字符串是否在另一个字符串中出现,其方法原型为:
int java.lang.String.indexOf(String arg0)。
如果字符串arg0出现在源字符串中,返回arg0在源字符串中首次出现的位置。
Java程序:
public class Main { 。
public static void main(String[] args) {。
String key = "wo";。
char[] arr = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'};。
String source= String.valueOf(arr);。
if(source.indexOf(key) >= 0) {。
System.out.printf("\"%s\" 中包含 \"%s\"", source, key);。
}
else {
System.out.printf("\"%s\" 中不包含 \"%s\"", source, key);。
}
}
运行测试:
"Hello, world!" 中包含 "wo"。