public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
System.out.println(demo.myIndexOf("12345678", '3'));
}
//模拟String字符串的indexof()方法
public int myIndexOf(String str,char c){
//判断是否为null
if(str == null){
throw new RuntimeException("IllegalArgumentException");
}
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if(chars[i] == c){
return i;
}
}
return -1 ;
}
}
|