indexof java_java indexOf()简单字符查找实例
java indexof()簡(jiǎn)單字符查找實(shí)例
int indexof(string ch);
就是查找字符/字符串ch在index以后的位置,如果沒有找到返回-1;index可以有可以沒有,沒有時(shí)默認(rèn)為0。
eg: string str="liuzheliuxing";
system.out.println((int)'i')???????????????????????? // i的ascii
system.out.println(str.indexof(105,0));?? // i的ascii是105
結(jié)果是:1
eg: string str="liuzheliuxing";
system.out.println(str.indexof('i',2));
結(jié)果:7
應(yīng)用:判斷參數(shù)是不是數(shù)字類型
public boolean isnumber(string str){string number="103194825611";
for (int i?? =?? 0; i< str.length();?? i )?? {
if(number.indexof(str.charat(i))?? ==?? -1)?? {
return false;
}
}
return true;
}
看個(gè)實(shí)例
public class mainclass
{
public static void main( string args[] )
{
string letters = "abcdefghijklmabcdefghijklm";
system.out.printf(
"'c' is located at index %dn", letters.indexof( 'c' ) );
system.out.printf(
"'a' is located at index %dn", letters.indexof( 'a', 1 ) );
system.out.printf(
"'$' is located at index %dnn", letters.indexof( '$' ) );
}
}
總結(jié)
以上是生活随笔為你收集整理的indexof java_java indexOf()简单字符查找实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sap sdi mysql_SAP HA
- 下一篇: python 无序列表中第k大元素_Py