企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# indexof() indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。 ## 提示和注释 注释:indexOf() 方法对大小写敏感! 注释:如果要检索的字符串值没有出现,则该方法返回 -1 ## 实例 在本例中,我们将在 "Hello world!" 字符串内进行不同的检索: ``` <pre class="calibre13">``` <span class="token"><</span>script type<span class="token">=</span><span class="token4">"text/javascript"</span><span class="token">></span> <span class="token2">var</span> str<span class="token">=</span><span class="token4">"Hello world!"</span> document<span class="token1">.</span><span class="token3">write</span><span class="token1">(</span>str<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">"Hello"</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">"<br />"</span><span class="token1">)</span> document<span class="token1">.</span><span class="token3">write</span><span class="token1">(</span>str<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">"World"</span><span class="token1">)</span> <span class="token">+</span> <span class="token4">"<br />"</span><span class="token1">)</span> document<span class="token1">.</span><span class="token3">write</span><span class="token1">(</span>str<span class="token1">.</span><span class="token3">indexOf</span><span class="token1">(</span><span class="token4">"world"</span><span class="token1">)</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> ``` ``` 以上代码的输出: ``` <pre class="calibre14">``` <span class="token5">0</span> <span class="token">-</span><span class="token5">1</span> <span class="token5">6</span> ``` ```