当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。