當前位置:首頁 » 編程語言 » java判斷字元串包含字元串

java判斷字元串包含字元串

發布時間: 2023-07-06 12:54:39

『壹』 java怎樣判斷一個字元串中的某個字元或字元串包含於另一個字元串

完全可以利用java系統給的函數indexof(),來判斷一個字元中是否包含另一個字元串:
列如:
String i = 「songfeilong」;
if(i.indexos("s")>0){
System.out.println("包含s字元串");
}
else{
System.out.println("不包含s字元串");

}
indexof()函數返回的是一個整數

『貳』 java判斷字元串中是否包含特定字元串

java判斷字元串中包含特定字元串方法:

使用正則表達式進行判斷。

源代碼:

publicclassTest{

publicstaticvoidmain(String[]args){
Stringstr="HelloWorld";//待判斷的字元串
Stringreg=".*ll.*";//判斷字元串中是否含有特定字元串ll
System.out.println(str.matches(reg));
}

}

『叄』 java中怎麼判斷一個字元串數組中包含某個字元或字元串

packagetest;

publicclassTest{

publicstaticvoidmain(String[]args){

Stringstr="ab";

System.out.println(isStr(str).toString());

}


/**

*判斷一個字元串數組中包含某個字元或字元串:返回一個boolean:參數判斷的字元

*1.定義一個字元串數組

*2.遍歷這個數組

*3.判斷要指定的字元串是否包含在字元串數組內

*4.如果包含返回true,否則返回false

*/

publicstaticBooleanisStr(Stringstr){

Stringarray[]={"a","b","c","hello"};

for(inti=0;i<array.length;i++){

if(str.equals(array[i])){

returntrue;

}

}

returnfalse;

}

}

『肆』 java string 判斷一個字元串里是否包含另一個字元串,忽略大小寫

Java中判斷一個字元串里是否包含另一個字元串,只需要使用indexOf方法查看返回值是否非負數,如果是,則說明存在(該返回值代表第一次找到匹配時的索引坐標)。如果要忽略大小寫,則可以使用將兩個字元串都轉為大寫(或小寫)之後再判斷的方法。

『伍』 java中怎麼判斷一個字元串中包含某個字元或字元串

方法:

使用String類的indexOf()方法可以判斷一個字元串是否在另一個字元串中出現,其方法原型為:

intjava.lang.String.indexOf(Stringarg0)


如果字元串arg0出現在源字元串中,返回arg0在源字元串中首次出現的位置。


Java程序:

publicclassMain{
publicstaticvoidmain(String[]args){
Stringkey="wo";
char[]arr={'H','e','l','l','o',',','','w','o','r','l','d','!'};
Stringsource=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"
熱點內容
超級訪問羅大佑 發布:2025-07-11 07:43:33 瀏覽:387
邁騰有什麼安全配置 發布:2025-07-11 07:42:40 瀏覽:644
c語言字元逆序 發布:2025-07-11 07:41:57 瀏覽:923
怎麼配置交換機的console密碼 發布:2025-07-11 07:41:57 瀏覽:4
東芝存儲卡視頻 發布:2025-07-11 07:41:55 瀏覽:541
cs16為什麼搜不到區域網伺服器 發布:2025-07-11 07:41:21 瀏覽:913
php項目如何上傳伺服器 發布:2025-07-11 07:35:35 瀏覽:182
壓縮機4井 發布:2025-07-11 07:34:57 瀏覽:726
win7訪問共享 發布:2025-07-11 07:34:48 瀏覽:279
telnetlinux退出 發布:2025-07-11 07:29:46 瀏覽:791