當前位置:編程學習大全網 - 網站源碼 - string 判斷是否有某個字符串

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);

}

}

}

  • 上一篇:是不是6點以後就不能在微信官方賬號充值燃佛能量了?
  • 下一篇:為什麽小米12pro耗電快
  • copyright 2024編程學習大全網