Javascript 同一输入返回-1和7的索引

Javascript 同一输入返回-1和7的索引,javascript,Javascript,iwsgroupware.indexOfhttp://iwsgroupware; 他回来了 -一, 鉴于http://iwsgroupware.indexOfiwsgroupware; 他回来了 七, 为什么会这样?这两个输入不一样 ("iwsgroupware").indexOf("http://iwsgroupware"); 因为iwsgroupware不包含字符串http://iwsgroupware,返回-1。另一方面, ("http://iwsgroupware").indexOf(

iwsgroupware.indexOfhttp://iwsgroupware; 他回来了

-一,

鉴于http://iwsgroupware.indexOfiwsgroupware; 他回来了

七,


为什么会这样?

这两个输入不一样

("iwsgroupware").indexOf("http://iwsgroupware");
因为iwsgroupware不包含字符串http://iwsgroupware,返回-1。另一方面,

("http://iwsgroupware").indexOf("iwsgroupware");
http://iwsgroupware 不包含字符串iwsgroupware,因此返回其索引7

首先,你必须了解indexOf

答案是:

在iwsgroupware.indexO中fhttp://iwsgroupware;

iwsgroupware不包含该字符串http://iwsgroupware.

所以它返回-1

何处为http://iwsgroupware.indexOfiwsgroupware;

http://iwsgroupware 包含字符串iwsgroupware


所以这里indexOf返回值7,它们不一样。indexOf的使用方式与haystack类似。indexOf'Pineer',因此,虽然蓝色可以在蓝莓中找到,但蓝莓不会在蓝色中找到。请首先了解indexOf操作的实际作用。搜索此字符串。indexOf或此字符串的原因与3/1=3而1/3=0的原因相同;当您更改值时,结果也会更改。谢谢您的回复@大卫,我问这个问题是因为出于某种原因,3*1和1*3是一样的。
returns the position of the first occurrence of a specified value in a string.