Java - String lastIndexOf() 方法



Java String lastIndexOf() 方法用於檢索指定字元在當前字串中最後一次出現的索引位置。索引指的是字串中字元的位置。

索引範圍從0開始,到length() -1結束。第一個字元的值表示第 0 個索引,第二個字元的值表示第 1 個索引,以此類推。此方法接受不同的引數。

lastIndexOf() 方法有四個多型變體,具有不同的引數,例如 char、fromIndex 和字串。以下是所有多型變體的語法。

語法

以下是Java String lastIndexOf() 方法的語法:

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

引數

  • ch − 這是字元的 Unicode 碼點值。

  • fromIndex − 這是索引的起始位置。

  • str − 這是字串的值。

返回值

  • 此方法返回在該字串中指定字元最後一次出現的索引。// 第一個

  • 此方法返回在該字串中指定字元最後一次出現的索引,從指定的索引開始向後搜尋。// 第二個

  • 此方法返回在該字串中指定子字串最右邊一次出現的索引。// 第三個

  • 此方法返回在該字串中指定子字串最後一次出現的索引,從指定的索引開始向後搜尋。// 第四個

示例

如果給定的字元值在當前字串中沒有出現,則 lastIndexOf() 方法返回-1

在下面的程式中,我們使用值“Hello”例項化字串類。使用lastIndexOf() 方法,我們嘗試在當前字串中檢索字元'p'最後一次出現的索引值。

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("Hello");
      System.out.println("The given string is: " + str);
      
      //initialize the char value
      char ch = 'p';
      System.out.println("The given char value is: " + ch);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + ch + "' is: " + str.lastIndexOf(ch));
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string is: Hello
The given char value is: p
The position of the 'p' is: -1

示例

如果我們傳遞 char 和 fromIndex 作為引數給方法,此方法將返回在該字串中指定字元最後一次出現的索引。

在下面的示例中,我們建立一個值為“HelloWorld”的字串類物件。然後,使用lastIndexOf() 方法,我們嘗試在指定fromIndex 5的字串中檢索字元“o”的位置。

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("HelloWorld");
      System.out.println("The given string is: " + str);
      
      //initialize the char value and fromIndex value
      char ch = 'o';
      int fromIndex = 5;
      System.out.println("The given char and fromIndex values are: " + ch + " and " + fromIndex);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + ch + "' is: " + str.lastIndexOf(ch, fromIndex));
   }
}

輸出

以下是上述程式的輸出:

The given string is: HelloWorld
The given char and fromIndex values are: o and 5
The position of the 'o' is: 4

示例

如果我們傳遞字串作為引數給方法,此方法將返回字串在當前字串中的位置。

在這個程式中,我們建立一個值為“TutorialsPoint”的字串字面量。使用lastIndexOf() 方法,我們嘗試在給定字串值中檢索指定字串“als”最後一次出現的位置。

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //create string literal
      String str = "TutorialsPoint";
      System.out.println("The given string is: " + str);
      
      //initialize argument string
      String str_argu = "als";
      System.out.println("The argument string is: " + str_argu);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + str_argu + "' is: " + str.lastIndexOf(str_argu));
   }
}

輸出

上述程式產生以下輸出:

The given string is: TutorialsPoint
The argument string is: als
The position of the 'als' is: 6

示例

如果我們傳遞字串和 fromIndex 作為引數給方法,此方法將返回在該字串中最後一次出現的索引。

在下面的程式中,我們使用值“Java Programming Language”例項化字串類。使用lastIndexOf() 方法,我們嘗試在指定fromIndex 10的當前字串中檢索字串引數的位置。

import java.lang.*;
public class LastIndex {
   public static void main(String[] args) {
      
      //instantiate the  string class
      String str = new String("Java Programming Language");
      System.out.println("The given string is: " + str);
      
      //initialize argument string and fromIndex value
      String str_argu = "Programming";
      int fromIndex = 10;
      System.out.println("The given argument string and fromIndex values are: " + str_argu + " and " + fromIndex);
      
      //using the lastIndexOf() method
      System.out.println("The position of the '" + str_argu + "' is: " + str.lastIndexOf(str_argu, fromIndex));
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string is: Java Programming Language
The given argument string and fromIndex values are: Programming and 10
The position of the 'Programming' is: 5
java_lang_string.htm
廣告
© . All rights reserved.