在Java中,“indexOf”方法具有以下形式:
1.`intindexof(intch)`:返回指定字符在字符串中第一次出现的索引位置,如果未找到该字符,则返回-1。
2.`intindexof(intch,intfromindex)`:从指定的索引位置返回指定字符在字符串中第一次出现的索引位置,如果未找到该字符,则返回-1。
3.`intindexof(stringstr)`:返回指定字符串在字符串中第一次出现的索引位置,如果找不到该字符串,则返回-1。
4.`intindexof(stringstr,intfromindex)`:从指定的索引位置返回指定字符串在字符串中第一次出现的索引位置,如果找不到该字符串,则返回-1。
该示例使用:
“`java
stringstr=“HelloWorld“;
intindex1=str。的索引(“o”);
系统。出去。println(索引1);//输出:4
intindex2=str。索引(‘o‘,5);
系统。出去。println(索引2);//输出:7
int索引3=str。(“世界”)索引;
系统。出去。println(索引3);//输出:6
intindex4=str。索引(“世界”,7);
系统。出去。println(索引4);//输出:-1
"`