Java中String类下indexOf方法的使用

str.indexOf(String substr);



用来查询str中字符substr的位置,起始位置为0,若str中不存在substr,则返回-1 。

例:

public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "1234567890";
int index = str.indexOf("1");
System.out.println(index);
}

}

执行结果为:0

public class Test1 {

/**
*
@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "1234567890";
int index = str.indexOf("1");
System.out.println(index);
}

}

执行结果为:-1 。
 


 

posted @ 2012-02-19 14:46  HANNN  阅读(479)  评论(1编辑  收藏  举报