indexof 为相同的输入返回 -1 和 7

indexOf returning -1 and 7 for the same input

本文关键字:返回 输入 indexof      更新时间:2023-09-26

("iwsgroupware").indexOf("http://iwsgroupware"); 正在返回

-1

("http://iwsgroupware").indexOf("iwsgroupware");正在回归

7

为什么会这样?

这两个输入并不相同。

("iwsgroupware").indexOf("http://iwsgroupware");

由于"iwsgroupware"不包含字符串"http://iwsgroupware",因此返回 -1。另一方面

("http://iwsgroupware").indexOf("iwsgroupware");

"http://iwsgroupware"确实包含字符串"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