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
廣告