JavaScript对象:JavaScript startsWith() 方法

返回到:JavaScript对象:JavaScript String 对象

定义和用法

startsWith() 方法用于检测字符串是否以指定的子字符串开始。

如果是以指定的子字符串开头返回 true,否则 false。

startsWith() 方法对大小写敏感。

所有主要浏览器都支持 slice() 方法。

语法

string.startsWith(searchvalue, start)

参数值

参数描述
searchvalue必需,要查找的字符串。
start可选,查找的开始位置,默认为 0。

返回值

类型描述
Boolean如果字符串是以指定的子字符串开头返回 true,否则 false。

技术细节

JavaScript 版本:ECMAScript 6

例子

查看从第 6 个索引位置是否以 “world” 开头。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程(Web176.com)</title>
</head>
<body>

<p>点击按钮查看字符串是否以指定的子字符串开头。</p>

<button onclick="myFunction()">点我</button>

<p id="demo"></p>

<p><strong>注意:</strong> IE 11 及更早版本不支持 startsWith() 方法 。</p>
	
<script>
function myFunction() {
  var str = "Hello world, welcome to the Web176.com.";
  var n = str.startsWith("world", 6);
  document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/jsarrtips/3028.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年8月23日 下午2:35
下一篇 2021年8月23日 下午2:42

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注