方法1 =》 substring
let str = 'iotstudio666'
console.log(str.substring(0));
console.log(str.substring(3));
console.log(str.substring(3, 8));
方法2 =》substr
console.log(str.substr(0));
console.log(str.substr(0, 8));
console.log(str.substr(-5, 3));
方法3 =》 slice
// 方法3 =》 slice
// slice()方法返回的子串包括start处的字符,但不包括end处的字符
console.log(str.slice(0));//iotstudio666
console.log(str.slice(0,6));//iotstu
完整测试代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let str = 'iotstudio666'
console.log(str.substring(0));
console.log(str.substring(3));
console.log(str.substring(3, 8));
console.log('---------------------------------');
console.log(str.substr(0));
console.log(str.substr(0, 8));
console.log(str.substr(-5, 3));
console.log('---------------------------------');
console.log(str.slice(0));
console.log(str.slice(0,6));
</script>
</body>
</html>
|