javascript string.indexOf() 和 string.lastIndexOf() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25356825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:25:24  来源:igfitidea点击:

Difference between string.indexOf() and string.lastIndexOf()?

javascript

提问by Asieh hojatoleslami

What's the difference between string.indexOf()and string.lastIndexOf()in JavaScript?

JavaScript 中的string.indexOf()和 有什么区别string.lastIndexOf()

var temp = state.indexOf("tmp");    
var temp = state.lastIndexOf("tmp");

回答by Iswanto San

From MDN :

来自 MDN :

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value

indexOf() 方法返回指定值第一次出现的调用 String 对象内的索引, lastIndexOf() 方法返回指定值最后一次出现的调用 String 对象内的索引

So the indexOfwill return first occurrence of the value and the lastIndexOfwill return the last occurence of the value.

因此indexOf将返回该值的第一次出现并lastIndexOf返回该值的最后一次出现。

Example (copied from MDN):

示例(从 MDN 复制):

var anyString = "Brave new world";
var a = anyString.indexOf("w")); // result = 8
var b = anyString.lastIndexOf("w")); // result 10

Both of the method return -1 if the value is not found

如果未找到该值,则两个方法都返回 -1

More :

更多的 :

回答by Manu Benjamin

string.indexOf() method returns the position of the first occurrence of a specified value in a string.

string.indexOf() 方法返回指定值在字符串中第一次出现的位置。

string.lastIndexOf() method returns the position of the last occurrence of a specified value in a string.

string.lastIndexOf() 方法返回指定值在字符串中最后一次出现的位置。

回答by Girish

indexOf()function is match first occurance, LastindexOf()function is match last occurance

indexOf()功能是匹配第一次出现,LastindexOf()功能是匹配最后一次出现

Both function are using for finding index in Stringor Array;

这两个函数都用于在StringArray 中查找索引;

sample code with Stringoperation

String操作的示例代码

var state = "abcdtmpefgtmp";
var temp =state.indexOf("tmp"); //match first occurance
console.log(temp);
//>4

var temp =state.lastIndexOf("tmp"); //match last occurance
console.log(temp);
//>10

sample code with Arrayoperation

Array操作的示例代码

var state = ["abc", "tmp", "efg", "tmp"];
var temp =state.indexOf("tmp");
console.log(temp);
//>1
var temp = state.lastIndexOf("tmp");
console.log(temp);
//>3

回答by Devarsh Desai

The lastIndexOf() method returns the position of the last occurrence of a specified value in a string, where as indexOf() returns the position of the first occurrence of a specified value in a string.

lastIndexOf() 方法返回指定值在字符串中最后一次出现的位置,而 indexOf() 返回指定值在字符串中第一次出现的位置。

In the following example:

在以下示例中:

 function myFunction() {
     var str = "Hello planet earth, you are a great planet.";
     var n = str.lastIndexOf("planet");
     var n2 = str.indexOf("planet");

 }

nwill return 36 and n2will return 6

n将返回 36n2将返回 6

Please let me know if you have any questions!

请让我知道,如果你有任何问题!

回答by Mritunjay

Basically indexOf& lastIndexOfare native javascript functions. Both functions can be applied on Stringsas well as Arrays.

基本上indexOf&lastIndexOf是原生的 javascript 函数。这两个函数都可以应用于Strings以及Arrays

The indexOfreturn the very first index where the element matched.

indexOf回哪里元素相匹配的第一个指标。

On the other hand lastIndexOfreturns the last index where the element matched.

另一方面,lastIndexOf返回元素匹配的最后一个索引。

"myname".indexOf('m') //returns 0

"myname".lastIndexOf('m') //returns 4