lastindexof
时间: 2023-09-01 18:10:51 浏览: 94
indexOf 和 lastIndexOf 使用示例介绍
lastIndexOf is a built-in function in many programming languages that returns the index of the last occurrence of a specific element or substring in a given string or array. It starts searching from the end of the string or array and returns -1 if the element or substring is not found.
For example, in JavaScript:
```
let str = "hello world";
console.log(str.lastIndexOf("o")); // outputs 7
console.log(str.lastIndexOf("l")); // outputs 9
console.log(str.lastIndexOf("x")); // outputs -1
```
In Java:
```
String str = "hello world";
System.out.println(str.lastIndexOf("o")); // outputs 7
System.out.println(str.lastIndexOf("l")); // outputs 9
System.out.println(str.lastIndexOf("x")); // outputs -1
```
阅读全文