原文: https://howtodoinjava.com/java/string/java-string-indexof-method-example/

Java 字符串indexOf()方法返回给定参数字符或字符串的索引。 如果在字符串中找不到参数,则方法返回-1。 字符串的索引计数器从零开始。

Java String.indexOf()方法语法

String.indexOf()方法具有四种重载形式:

序号 方法语法 描述
1. int indexOf(String substring) 返回给定子字符串的索引位置
2. int indexOf(String substring, int fromIndex) fromIndex位置返回给定子字符串的索引位置
3. int indexOf(int ch) 返回给定char值的索引位置
4. int indexOf(int ch, int fromIndex) 返回给定char值和fromIndex位置的索引位置

不允许使用null参数

不允许将null参数传递给indexOf()方法。 这将导致NullPointerException异常。

  1. String blogName = "howtodoinjava.com";
  2. System.out.println( blogName.indexOf(null) );
  3. //Program output
  4. Exception in thread "main" java.lang.NullPointerException
  5. at java.lang.String.indexOf(String.java:1705)
  6. at java.lang.String.indexOf(String.java:1685)
  7. at com.StringExample.main(StringExample.java:9)

1. Java String.indexOf(String substring)示例

Java 程序使用indexOf(String substring)方法在给定的字符串对象中查找子字符串的索引。

  1. public class StringExample
  2. {
  3. public static void main(String[] args)
  4. {
  5. String blogName = "howtodoinjava.com";
  6. System.out.println( blogName.indexOf("java") ); //9
  7. System.out.println( "hello world".indexOf("world") ); //6
  8. System.out.println( "hello world".indexOf("earth") ); //-1
  9. }
  10. }

程序输出。

  1. 9
  2. 6
  3. -1

2. Java String.indexOf(String substring, int fromIndex)示例

Java 程序,使用给定的indexOf(String substring, int fromIndex, int fromIndex)方法,在给定的fromIndex中查找给定字符串对象中的substring索引。

请注意,找到子字符串时,索引计数仅从 0 索引开始,并且仅从字符串开头开始。

  1. public class StringExample
  2. {
  3. public static void main(String[] args)
  4. {
  5. String blogName = "howtodoinjava.com";
  6. System.out.println( blogName.indexOf("java", 5) ); //9
  7. System.out.println( "hello world".indexOf("world", 6) ); //6
  8. System.out.println( "hello world".indexOf("world", 2) ); //6
  9. System.out.println( "hello world".indexOf("world", 10) ); //-1
  10. }
  11. }

程序输出:

  1. 9
  2. 6
  3. 6
  4. -1

3. Java String.indexOf(char ch)示例

Java 程序使用indexOf(char ch)方法在给定的字符串对象中查找给定字符'ch'的索引。

  1. public class StringExample
  2. {
  3. public static void main(String[] args)
  4. {
  5. String blogName = "howtodoinjava.com";
  6. System.out.println( blogName.indexOf('j') ); //9
  7. System.out.println( "hello world".indexOf('w') ); //6
  8. System.out.println( "hello world".indexOf('k') ); //-1
  9. }
  10. }

程序输出:

  1. 9
  2. 6
  3. -1

4. Java String.indexOf(int ch, int fromIndex)示例

Java 程序使用给定的indexOf(String substring, int fromIndex)方法从给定的fromIndex位置开始在给定的字符串对象中查找字符'ch'的索引。

请注意,找到字符后,索引计数仅从 0 索引开始,并且仅从字符串的开头开始。

  1. public class StringExample
  2. {
  3. public static void main(String[] args)
  4. {
  5. String blogName = "howtodoinjava.com";
  6. System.out.println( blogName.indexOf('j', 4) ); //9
  7. System.out.println( "hello world".indexOf('w', 2) ); //6
  8. System.out.println( "hello world".indexOf('w', 6) ); //6
  9. System.out.println( "hello world".indexOf('k') ); //-1
  10. }
  11. }

程序输出:

  1. 9
  2. 6
  3. 6
  4. -1

学习愉快!

Java String文档