indexOf()函数是一个字符串方法,用于返回指定字符串在原字符串中首次出现的位置。如果指定的字符串不存在于原字符串中,则返回-1。

语法: string.indexOf(searchValue[, fromIndex])

参数: searchValue:要查找的字符串值。 fromIndex:从该索引位置开始查找searchValue。如果省略此参数,则默认从字符串开头开始查找。

示例: var str = "Hello World!"; var n = str.indexOf("World"); console.log(n); // 输出 6

在上面的示例中,indexOf()函数返回“World”字符串在“Hello World!”中首次出现的位置,即6。

indexOf函数

原文地址: https://www.cveoy.top/t/topic/1Dp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录