首页 > 编程知识 正文

indexof函数用法,js中indexof的用法

时间:2023-05-05 00:33:03 阅读:198300 作者:1292

1.indexOf(int ch) 在给定字符串中查找字符(ASCII),找到返回字符数组所对应的下标找不到返回-12.indexOf(String str)在给定符串中查找另一个字符串。。。3.indexOf(int ch,int fromIndex)从指定的下标开始查找某个字符,查找到返回下标,查找不到返回-14.indexOf(String str,int fromIndex)从指定的下标开始查找某个字符串。。。 public class Test { public static void main(String[] args) { char[] ch= {'a','b','c','h','e','l','l','o'}; String str=new String(ch); //1.indexOf(int ch) str.indexOf(104); System.out.println(str.indexOf(104));//h所在下标为3 //2.indexOf(String str) str.indexOf("hell"); System.out.println(str.indexOf("hell"));//3 //3.indexOf(int ch,int fromIndex) str.indexOf(101, 4);//4 System.out.println(str.indexOf(101, 4)); str.indexOf(101,5);//没有查找到返回-1 System.out.println(str.indexOf(101,5)); //4.indexOf(String str,int fromIndex) str.indexOf("che", 0);//等价于str.indexOf("che") System.out.println(str.indexOf("che", 0));//2 }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。