Java String indexof示例
时间:2020-02-23 14:35:13 来源:igfitidea点击:
String的indexof方法用于查找任何字符或者子字符串的索引。
indexof方法可以将char或者string作为参数拍摄,也可以提供fromIndex(索引之后我们要使用索引方法)。
方法:
public int indexof(int ch)返回字符串中第一次出现字符的索引
Public Int IndexOf(int ch,int flowindex)返回字符串内的第一次出现字符的索引,从特定索引"fromIndex"公共int indexof(string)返回字符串公共int index中的子字符串的第一次出现索引(int ch,int fromIndex)从特定索引"fromIndex"开始,返回字符串内第一次出现子字符串的索引
如果字符串中不存在char或substring,则所有indexOf方法都返回-1
字符串索引示例:
package org.igi.theitroad;
public class StringIndexOfExample {
/*
* @ Author : igi Mandliya
*/
public static void main(String args[]) {
String str="Hello world from theitroad";
//Using index of method with character
System.out.println("---------------------------------------------------");
System.out.println("Using indexOf method with character");
System.out.println("---------------------------------------------------");
System.out.println("Index of character b in "Hello world from theitroad" is : "+str.indexOf('b'));
System.out.println();
String str1="from";
System.out.println("---------------------------------------------------");
System.out.println("Using indexOf method with fromIndex with character");
System.out.println("---------------------------------------------------");
System.out.println("Index of character o from index 6 in "Hello world from theitroad" is : "+str.indexOf('o',6));
System.out.println();
//Using index of method with String
System.out.println("---------------------------------------------------");
System.out.println("Using indexOf method with String");
System.out.println("---------------------------------------------------");
System.out.println("Index of String "from" in "Hello world from theitroad" is : "+str.indexOf(str1));
System.out.println();
String howDo="How do you do";
System.out.println("---------------------------------------------------");
System.out.println("Using indexOf method with fromIndex with String");
System.out.println("---------------------------------------------------");
System.out.println("Index of String "do" from index 9 in "How do you do" is : "+howDo.indexOf("do",9));
System.out.println();
System.out.println("---------------------------------------------------");
System.out.println("If String or char is not available");
System.out.println("---------------------------------------------------");
System.out.println("Index of "from" in "How do you do" is : "+howDo.indexOf("from"));
}
}
运行上面的程序时,我们将得到以下输出:
------------------- Using indexOf method with character ------------------- Index of character b in "Hello world from theitroad" is : 22 ------------------- Using indexOf method with fromIndex with character ------------------- Index of character o from index 6 in "Hello world from theitroad" is : 7 ------------------- Using indexOf method with String ------------------- Index of String "from" in "Hello world from theitroad" is : 12 ------------------- Using indexOf method with fromIndex with String ------------------- Index of String "do" from index 9 in "How do you do" is : 11 ------------------- If String or char is not available ------------------- Index of "from" in "How do you do" is : -1

