string 判断是否有某个字符串

如题所述

查看String的API文档 , 里面有1个方法如下

public boolean contains(CharSequence s)

    当且仅当此字符串包含指定的 char 值序列时,返回 true。

比如"BCD".contains"C";//结果是true

参考代码如下


public class Test {
public static void main(String[] args) {
String word1="java";
String word2="JaVa";
String str ="快乐学java!~";
if(str.contains(word1)) {
System.out.println("Yes:字符串:"+str+"\t包含字符串:"+word1);
}else {
System.out.println("No:字符串:"+str+"\t不包含字符串:"+word1);
}

if(str.contains(word2)) {
System.out.println("Yes:字符串:"+str+"\t包含字符串:"+word2);
}else {
System.out.println("No :字符串:"+str+"\t不包含字符串:"+word2);
}

System.out.println("忽略大小写的情况下");
//大家都转换成小写, 或者大写, 进行判断 , 这样就可以忽略大小写
if(str.toLowerCase().contains(word2.toLowerCase())) {
System.out.println("Yes:字符串:"+str+"\t包含字符串:"+word2);
}else {
System.out.println("No:字符串:"+str+"\t不包含字符串:"+word2);
}

}
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网