javascript - Difference between string.indexOf() and string.lastIndexOf()? -
what's difference between string.indexof()
, string.lastindexof()
in javascript?
var temp = state.indexof("tmp"); var temp = state.lastindexof("tmp");
from mdn :
the indexof() method returns index within calling string object of first occurrence of specified value, lastindexof() method returns index within calling string object of last occurrence of specified value
so indexof
return first occurrence of value , lastindexof
return last occurence of value.
example (copied mdn):
var anystring = "brave new world"; var = anystring.indexof("w")); // result = 8 var b = anystring.lastindexof("w")); // result 10
both of method return -1 if value not found
more :
Comments
Post a Comment