當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java String lastIndexOf()用法及代碼示例


Java String 類 lastIndexOf() 方法返回給定字符值或子字符串的最後一個索引。如果未找到,則返回 -1。索引計數器從零開始。

簽名

Java 中有四種類型的 lastIndexOf() 方法。方法的簽名如下:

沒有。方法描述
1int lastIndexOf(int ch)它返回給定 char 值的最後一個索引位置
2int lastIndexOf(int ch, int fromIndex)它返回給定字符值和索引的最後一個索引位置
3int lastIndexOf(字符串子串)它返回給定子字符串的最後一個索引位置
4int lastIndexOf(String substring, int fromIndex)它返回給定子字符串和索引的最後一個索引位置

參數

ch:char 值,即單個字符,例如'a'

fromIndex:index 返回字符值或子字符串索引的位置

substring:substring 要在這個字符串中搜索

返回

字符串的最後一個索引

內部實施

下麵介紹四種lastIndexOf()方法的內部實現。

下麵介紹四種lastIndexOf()方法的內部實現。

1. int lastIndexOf(int ch)

public int lastIndexOf(int ch) 
{
   return lastIndexOf(ch, value.length - 1);
}

2. int lastIndexOf(int ch, int fromIndex)

public int lastIndexOf(int ch, int fromIndex) 
{
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) 
{
// handling most of the cases here 
// negative value means invalid code point
final char[] val = this.value;
int j = Math.min(fromIndex, val.length - 1);
for (; jj >= 0; j--) 
{
if (val[i] == ch) 
{
return j;
}
}
return -1;
} 

else 
{
return lastIndexOfSupplementary(ch, fromIndex);
}
}

// internal implementation of the method lastIndexOfSupplementary()
private int lastIndexOfSupplementary(int c, int fIndex) 
{
if (Character.isValidCodePoint(c)) 
{
final char[] val = this.value;
char h = Character.highSurrogate(c);
char l = Character.lowSurrogate(c);
int j = Math.min(fIndex, value.length - 2);
for (; j >= 0; j--) 
{
if (val[j] == h && val[j + 1] == l) 
{
return j;
}
}
}
return -1;
}

3. int lastIndexOf(String subString)

public int lastIndexOf(String subString) 
{
return lastIndexOf(subString, value.length);
}

4. int lastIndexOf(String substring, int fromIndex)

public int lastIndexOf(String substring, int fromIndex) 
{
public int lastIndexOf(String str, int fromIndex) 
{
return lastIndexOf(value, 0, value.length, str.value, 0, str.value.length, fromIndex);
} 
}

// src             the characters that are being searched.
// srcOffset  provides the offset of the src string.
// srcCount  number of the source string.
// tar             the characters that are being searched for.
// fromIndex  the index to start search from.
static int lastIndexOf(char[] src, int srcOffset, int srcCount, String tar, int fromIndex) 
{
return lastIndexOf(src, srcOffset, srcCount, tar.value, 0, tar.value.length, fromIndex);
}


static int lastIndexOf(char[] src, int srcOffset, int srcCount, char[] tar, int tarOffset, int tarCount, int fromIndex) 
{
int rightIndex = srcCount - tarCount;
if (fromIndex < 0) 
{
    return -1;
}
if (fromIndex > rightIndex) 
{
    fromIndex = rightIndex;
}
// an Empty string is always a match.
if (tarCount == 0) 
{
    return fromIndex;
}

int lastStrIndex = tarOffset + tarCount - 1;
char lastStrChar = tar[strLastIndex];
int min = srcOffset + tarCount - 1;
int j = min + fromIndex;

startLookForLastChar:
while (true) 
{
while (j >= min && src[j] != lastStrChar) 
{
j = j - 1;
}
if (j < min) 
{
return -1;
}
int i = j - 1;
int begin = i - (tarCount - 1);
int m = lastStrIndex - 1;

while (i > begin) 
{
if (source[i--] != target[m--]) 
{
j = j + 1;
continue startLookForLastChar;
}
}
return begin - srcOffset + 1;
}
}

Java String lastIndexOf() 方法示例

文件名:LastIndexOfExample.java

public class LastIndexOfExample{
public static void main(String args[]){
String s1="this is index of example";//there are 2 's' characters in this sentence
int index1=s1.lastIndexOf('s');//returns last index of 's' char value
System.out.println(index1);//6
}}

輸出:

6

Java String lastIndexOf(int ch, int fromIndex) 方法示例

在這裏,我們通過指定 fromIndex 從字符串中找到最後一個索引。

文件名:LastIndexOfExample2.java

public class LastIndexOfExample2 {
	public static void main(String[] args) {
		String str = "This is index of example";
		int index = str.lastIndexOf('s',5);
		System.out.println(index);		
	}
}

輸出:

3

Java String lastIndexOf(String substring) 方法示例

它返回子字符串的最後一個索引。

文件名:LastIndexOfExample3.java

public class LastIndexOfExample3 {
	public static void main(String[] args) {		 
		String str = "This is last index of example";
		int index = str.lastIndexOf("of");
		System.out.println(index);		
	}
}

輸出:

19

Java String lastIndexOf(String substring, int fromIndex) 方法示例

它從 fromIndex 返回子字符串的最後一個索引。

文件名:LastIndexOfExample4.java

public class LastIndexOfExample4 {
	public static void main(String[] args) {		 
		String str = "This is last index of example";
		int index = str.lastIndexOf("of", 25);
		System.out.println(index);
		index = str.lastIndexOf("of", 10);
		System.out.println(index); // -1, if not found		
	}
}

輸出:

19
-1




相關用法


注:本文由純淨天空篩選整理自 Java String lastIndexOf()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。