("iwsgroupware").indexOf("http://iwsgroupware");
正在返回
-1
而("http://iwsgroupware").indexOf("iwsgroupware");
正在返回
7
为什么会这样?
("iwsgroupware").indexOf("http://iwsgroupware");
正在返回
-1
而("http://iwsgroupware").indexOf("iwsgroupware");
正在返回
7
为什么会这样?
两个输入不一样。
("iwsgroupware").indexOf("http://iwsgroupware");
由于"iwsgroupware"
不包含 String "http://iwsgroupware"
,因此返回 -1。另一方面,
("http://iwsgroupware").indexOf("iwsgroupware");
"http://iwsgroupware"
确实包含 String "iwsgroupware"
,因此返回其索引 (7)。
首先你必须了解 indexOf()。
答案是:
returns the position of the first occurrence of a specified value in a string.
在("iwsgroupware").indexOf("http://iwsgroupware");
iwsgroupware
不包含字符串http://iwsgroupware
。
所以它返回-1
然而("http://iwsgroupware").indexOf("iwsgroupware");
http://iwsgroupware
包含字符串iwsgroupware
。
所以这里 indexOf() 返回值7