更新時(shí)間:2022-06-17 10:41:38 來源:動(dòng)力節(jié)點(diǎn) 瀏覽5253次
動(dòng)力節(jié)點(diǎn)小編來告訴大家Java判斷字符串是否包含字符的方法:
(1)說明
Contains() 方法當(dāng)且僅當(dāng)字符串包含指定的 char 值序列時(shí)才返回 true
(2)聲明
public boolean contains(CharSequence s)
(3)返回值
如果字符串包含則返回真,否則返回假。
(4)實(shí)例
public static void main(String[] args) {
String str = "abc";
boolean status = str.contains("a");
if(status){
System.out.println ("included");
}else{
System.out.println ("not included");
}
}
(1)說明
indexof() 用于查找單詞在字符串中的位置,判斷字符串是否包含字符。
(2)聲明
int indexOf(int ch,int fromIndex)
(3)返回值
indexof 的返回值為 int
(4)實(shí)例
public static void main(String[] args) {
String str1 = "abcdefg";
int result1 = str1.indexOf("a");
if(result1 != -1){
System. Out. Println ("string STR contains substring" a "+ result1);
}else{
System. Out. Println ("string STR does not contain substring" a "+ result1);
}
}
以上就是Java判斷字符串是否包含字符的方法細(xì)節(jié)。如果大家想了解更多相關(guān)知識(shí),不妨來關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面的課程內(nèi)容從入門到精通,細(xì)致全面,很適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對大家能夠有所幫助哦。
相關(guān)閱讀
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743