Java lastIndexOf 方法

Java中的lastIndexOf 方法是String方法之一,用于返回指定字符串最后一次出现的索引位置。如果找不到指定的文本,此方法将返回-1。

在本文中,我们将通过示例演示如何在该编程语言中使用String lastIndexOf,其语法如下所示。

Java lastIndexOf 方法语法

Java编程语言提供了四种不同的字符串lastIndexOf方法来返回最后一次出现的索引位置。第一个方法接受字符作为参数,并返回指定字符最后一次出现的索引位置。

第二个方法接受字符和最后一个索引位置(End_index)作为第二个参数。此方法将从索引位置0搜索到End_index索引位置。然后,它返回指定字符最后一次出现的位置。

public int lastIndexOf(int ch); // It will return Integer 
String_Object.lastIndexOf(int ch); //In order to use in program

public int lastIndexOf(int ch, int End_Index);
String_Object.lastIndexOf(int ch, int End_Index)

下面的Java lastindexof方法接受字符串数据作为参数,并返回指定子字符串最后一次出现的索引位置。

第二个方法接受字符串数据和End_index。lastIndexOf方法将从索引位置0开始搜索到End_index索引位置,并返回指定子字符串的最后一次出现的位置。

public int lastIndexOf(String str); // It will return Integer 
String_Object.lastIndexOf(String str)

public int lastIndexOf(String str, int End_Index);
String_Object.lastIndexOf(String str, int Starting_Index)
  • String_Object:请指定有效的对象。
  • End_Index:请指定结束索引位置。如果您指定此值,该函数将从索引位置0开始搜索到此位置(而不是到末尾)。

注意:如果您将负值指定为End_Index,Java函数将返回-1。

Java lastIndexOf 方法示例

lastIndexOf 方法返回指定字符串最后一次出现的索引位置。此程序将帮助理解该方法。

package StringFunctions;

public class LastIndexOfMethod {
	public static void main(String[] args) {
		String str = "Tutorials On Java Programming";
		String str1 = "We are abc working at abc company";
		
		int a = str.lastIndexOf('i');
		int b = str.lastIndexOf('i', 10);
		int c = str.lastIndexOf('i', -10);
		int d = str.lastIndexOf('y');
		
		int e = str1.lastIndexOf("abc");
		int f = str1.lastIndexOf("abc", 21);
		int g = str1.lastIndexOf("abc", 29);
		int h = str1.lastIndexOf("abc", -25);
		
		System.out.println( "Last Index position of i = " + a);
		System.out.println( "Starting from 10 - Last Index position of i = " + b);
		System.out.println( "Start from -Ve Index - Last Index position of i = " + c);
		System.out.println( "Last Index position of y = " + d);
		System.out.println( "Last Index position of String abc = " + e);
		System.out.println( "Starting from 10 - Last Index position of String abc = " + f);
		System.out.println( "Starting from 25 - Last Index position of String abc = " + g);
		System.out.println( "Start from -Ve Index - Last Index position of String abc = " + h);
	}
}
Java lastIndexOf Method Example

此Java示例调用public int lastIndexOf (int ch)来查找最后出现的'i'的索引位置。

int a = str.lastIndexOf('i');

以下语句调用public int lastIndexOf (int ch, int End_Index)方法,以查找从索引位置0到10的'i'的索引位置。众所周知,i在索引位置10之前只出现一次。因此,它返回相同的值。

int b = str.lastIndexOf('i', 10);

接下来,我们将End_Index用作负值。

int c = str.lastIndexOf('i', -10);

在下一行中,我们在str中查找不存在的项“y”。它将调用public int lastIndexOf (int ch)来查找“y”的最后索引位置。由于未找到,它将输出-1。

int d = str.lastIndexOf('y');

它调用Java public int lastIndexOf (String str)方法来查找子字符串“abc”的索引位置。然后,它将最后一个索引位置值存储在变量e中。

int e = str1.lastIndexOf("abc");

在此字符串方法中,您应将空格计为一个字符。从上面的语句来看,尽管术语abc重复出现多次,但它返回了最后一次出现的索引位置。现在,让我们提供结束索引位置。

以下语句将调用public int lastIndexOf(String str, int End_Index)方法。它返回字符串abc从索引位置0到21的最后一次出现的位置。

int f = str1.lastIndexOf("abc", 21);

让我们将End_Index值更改为29。这意味着该方法返回字符串abc从0到29的最后一次出现的位置。

int g = str1.lastIndexOf("abc", 29);

最后,我们使用System.out.println语句打印输出。