當前位置:首頁 » 編程語言 » 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"
熱點內容
sql能力 發布:2025-02-09 10:43:50 瀏覽:982
編譯framework 發布:2025-02-09 10:42:11 瀏覽:382
五子棋對戰演算法 發布:2025-02-09 10:12:19 瀏覽:713
php樹菜單 發布:2025-02-09 10:04:10 瀏覽:360
linux保存ip 發布:2025-02-09 10:04:10 瀏覽:24
四川霜狼伺服器怎麼樣 發布:2025-02-09 10:02:44 瀏覽:146
Vs中h編譯選項是灰的 發布:2025-02-09 10:01:59 瀏覽:558
安卓43怎麼升級44 發布:2025-02-09 09:51:33 瀏覽:464
美國雲伺服器快還是香港快 發布:2025-02-09 09:34:33 瀏覽:989
怎麼解壓qq文件 發布:2025-02-09 09:18:14 瀏覽:581