<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
//字符串API:
const s = "Hello world!";
1.includes()返回的是boolean,而indexOf()返回的是找到元素的当前下标
// 只有一个参数时:
console.log(s.includes('Hello')) //true
//有两个参数:第二个参数是开始检索的位置
console.log(s.includes("Hello", 0)) //true
console.log(s.includes("Hello", 1)) //false
2.startsWhith() 用来判断字符串的开头是否是我们自己输入的字符
// 只有一个参数时:
console.log(s.startsWith('Hello')) //true
//有两个参数:第二个参数是开始检索的位置,下面的例子表示world!是否从从下表6开始的
console.log(s.startsWith('world!', 6)) //true
3.endsWith()用来判断字符串的结尾是否是我们自己输入的字符
// 只有一个参数时:
console.log(s.endsWith('!')) //true
//有两个参数:第二个参数是开始检索的位置,下面的例子表示world!是否从从下表6结束的
console.log(s.endsWith('H', 1)) //true
4.repeat 方法返回一个新字符串,表示将原字符串重复n 次。
console.log('x'.repeat(3)) // xxx
// 参数如果是正小数,会被向下取整
console.log('x'.repeat(3.2)) // xxx
// 参数如果是-1到0小数,会被向上取整为0。
console.log('x'.repeat(-0.2)) //什么也没有输出
// 参数如果是字符串,会被先转化为数字。
console.warn('ha'.repeat('2')); // haha;
/特殊情况:
参数如果是NaN,会被转化为0(string转Number失败)。
参数如果是-1及以下小数,会报错。
参数如果是Infinity,会报错。/5.padStart() 用于头部补全
const str = 'hello';
console.log(str.padStart(7, '*')); //**hello
6.padEnd() 用于尾部补全。
console.log(str.padEnd(7, '*')); //hello**
var num_str=' 1 12 37854 ';//长度为12的字符串
console.log(num_str.length)
7.trim() 方法用于删除字符串的头尾空格
console.log(num_str.trim().length)//10
8.trimStart():用于删除头部的空格
var num_str1=' 1 12 37854 ';
console.log(num_str1.trimStart(1).length)//11
9.trimEnd():用于删除尾部的空格
var num_str2=' 1 12 37854 ';
console.log(num_str2.trimEnd(1).length)//11
// 1.判断是不是, 不是数字,返回true就不是数字,返回false就是数字
// console.log(isNaN(1))//false
// console.log(isNaN(“asd”))//true
</script>
</head>
<body>
</body>
</html>