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
Difference between string.indexOf() and string.lastIndexOf()?
提问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 indexOf
will return first occurrence of the value and the lastIndexOf
will 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;
这两个函数都用于在String或Array 中查找索引;
sample code with String
operation
带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 Array
operation
带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");
}
n
will return 36
and n2
will return 6
n
将返回 36n2
将返回 6
Please let me know if you have any questions!
请让我知道,如果你有任何问题!
回答by Mritunjay
Basically indexOf
& lastIndexOf
are native javascript functions. Both functions can be applied on Strings
as well as Arrays
.
基本上indexOf
&lastIndexOf
是原生的 javascript 函数。这两个函数都可以应用于Strings
以及Arrays
。
The indexOf
return the very first index where the element matched.
该indexOf
回哪里元素相匹配的第一个指标。
On the other hand lastIndexOf
returns the last index where the element matched.
另一方面,lastIndexOf
返回元素匹配的最后一个索引。
"myname".indexOf('m') //returns 0
"myname".lastIndexOf('m') //returns 4