Java - String indexOf() 方法



Java String indexOf() 方法用于检索此字符串中指定字符第一次出现的索引值。索引指的是字符在字符串值中的位置。索引范围是从第 0 个索引到length()-1索引。

第一个字符表示第 0 个索引值,第二个字符表示第 1 个索引值,以此类推。indexOf() 方法接受不同的参数。

indexOf() 方法具有四个多态变体,它们具有不同的参数,例如 char、fromIndex 和 string(以下是所有多态变体的语法)。

语法

以下是Java String indexOf() 方法的语法:

public int indexOf(int ch) // first syntax
public int indexOf(int ch, int fromIndex)// second syntax
public int indexOf(String str)// third syntax
public int indexOf(String str, int fromIndex) // fourth syntax 

参数

  • ch − 这是一个字符(Unicode 代码点)。

  • fromIndex − 这是起始索引位置。

  • str − 这是一个字符串。

返回值

此方法返回此字符串中指定字符第一次出现的索引。

示例

如果给定的字符值在当前字符串中没有出现,则 indexOf() 方法返回-1

在下面的程序中,我们创建了一个值为“TutorialsPoint”的字符串字面量。然后,使用indexOf() 方法,我们尝试检索指定字符的索引值。

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      //create a string literal
      String str = "TutorialsPoint";
      System.out.println("The given string is: " + str);
      
      //initialize the char value
      char ch = 'x';
      System.out.println("The given char value is: " + ch);
      
      //using the indexOf() method
      System.out.println("The index of the '" + ch + "' is: " + str.indexOf(ch));
   }
}

输出

执行上述程序后,将产生以下结果:

The given string is: TutorialsPoint
The given char value is: x
The index of the 'x' is: -1

示例

如果我们将charfromIndex值作为参数传递给此方法,它将返回字符的索引。

在下面的示例中,我们使用值为“Hello world”的值实例化字符串类。使用indexOf() 方法,我们尝试在给定字符串中检索指定fromIndex 1处的字符‘W’

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      // instantiate the string class
      String str = new String("Hello World");
      System.out.println("The given string is: " + str);
      
      //initialize the char and fromIndex values
      char ch = 'W';
      int fromIndex = 1;
      System.out.println("The given char and fromIndex values are: " + ch + " and " + fromIndex);
      
      // using the indexOf() method
      System.out.print("The index of the '" + ch + "' is: " + str.indexOf(ch, fromIndex));
   }
}

输出

以下是上述程序的输出:

The given string is: Hello World
The given char and fromIndex values are: W and 1
The index of the 'W' is: 6

示例

如果我们将字符串作为参数传递给该方法,则 indexOf() 方法将返回指定字符串的索引位置。

在这个程序中,我们创建了一个值为“Java Programming”的字符串类对象。然后,使用indexOf() 方法,我们尝试检索当前字符串中指定字符串值“Programming”的索引。

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str = new String("Java Programming");
      System.out.println("The given string is: " + str);
      
      //initialize the string value
      String str1 = "Programming";
      System.out.println("The initialize substring is: " + str1);
      
      // using indexOf() method
      int index = str.indexOf(str1);
      System.out.println("The index of the '" + str1 + "' is: " + index);
   }
}

输出

上述程序产生以下输出:

The given string is: Java Programming
The initialize substring is: Programming
The index of the 'Programming' is: 5

示例

如果我们将字符串和 fromIndex作为参数传递给此方法,它将返回指定字符串的索引位置。

在这个例子中,我们创建了一个值为“Hello World”的字符串字面量。使用indexOf() 方法,我们尝试获取指定fromIndex 2处的字符串值“World”的索引。

import java.lang.*;
public class Index {
   public static void main(String[] args) {
      
      // create string literal
      String str = "Hello World";
      System.out.println("The given string is: " + str);
      
      //initialize the string and fromIndex values
      String str1 = "World";
      int fromIndex = 2;
      System.out.println("The given string and from index values are: " + str1 + " and " + fromIndex);
      
      // using indexOf() method
      int index = str.indexOf(str1, fromIndex);
      System.out.println("The index of the '" + str1 + "' is: " + index);
   }
}

输出

执行上述程序后,将产生以下输出:

The given string is: Hello World
The given string and from index values are: World and 2
The index of the 'World' is: 6
java_lang_string.htm
广告
© . All rights reserved.