查看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);
}
}
}