1.字符串恒定性
含义:字符串不可以被修改
注意:是字符串本身不可以被修改,不是变量的值不能被修改
上代码:
<!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 = '欢迎来到我的博客'
// (1)字符串类似于数组,也有长度和下标
console.log(str[0])//欢
console.log(str.length)//8
// (2)字符串不可以被直接修改
// (3)字符串所有的方法都不会修改字符串本身,而是得到新的字符串
//这里的replace中的值是这样改的('要改的','改成什么样的')
const str1=str.replace('欢迎来到我的博客','下次再来')
console.log(str1) //下次再来
</script>
</body>
</html>
2. indexof方法
变量名.indexOf('字符串') 获取 ‘字符串’ 在变量名中的首字母下标
如果字符串存在则返回首字母下标, 如果字符串不存在则返回固定值 -1
应用场景: 一般用户判断 str中是否有某个字符串 如果没有则返回-1,不是-1说明有
上代码:
<!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 = '欢迎来到我的博客页面'
// 1. str.indexOf('字符串') 获取 ‘字符串’ 在str中的首字母下标
// 如果字符串存在则返回首字母下标, 如果字符串不存在则返回固定值 -1
// 应用场景: 一般用户判断 str中是否有某个字符串 如果没有则返回-1,不是-1说明有
const index=str.indexOf('博客')
console.log(index) //6
const index1=str.indexOf('常来') //字符串不存在,返回-1
console.log(index1)
//2. 案例:判断用户是不是谷歌内核
if(navigator.userAgent.indexOf('chrome')){
location.href='https://chrome.google.com/webstore?hl=zh-CN'
}else{
alert('你用的是谷歌嘛?')
}
</script>
</body>
</html>
3.split方法
<!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>
// str.split('分隔符') 用分隔符切割字符串,得到切割之后的数组
// 应用场景 : 一般用于解析 网址的参数
let url = 'http://www.baidu.com?name=张三&age=20'
console.log(url.split())//['http://www.baidu.com?name=张三&age=20']
console.log(url.split('='))//['http://www.baidu.com?name', '张三&age', '20']
console.log(url.split('?'))//['http://www.baidu.com', 'name=张三&age=20']
</script>
</body>
</html>
4.substring方法 substring 截取字符串
语法: 字符串.substring(起始索引号,[结束索引号])
// 应用场景 : 把 手机号中间4个数字变成 ****
let phone='13243256543'
const telphone=phone.replace(phone.substring(3,7),'****')
console.log(telphone)
5.字符串其他方法了解
1. startsWith(检测字符,[检测位置])
检测是否以某个字符开头,返回布尔值
2.endsWith(检测字符,[字符串长度])
检测是否以某个字符结尾,返回布尔值
3.includes(搜索的字符,[检测位置])
判断一个字符串是否 包含 在另外一个字符串中,返回布尔值
4. trim() 去除字符串首尾空格
<!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>字符串其他方法</title>
</head>
<body>
<script>
// 字符串 String 其他方法
const str = '传智播客传递知识'
// 1. startsWith(检测字符,[检测位置])
// 检测是否以某个字符开头,返回布尔值
console.log(str.startsWith('传')) // true
console.log(str.startsWith('智')) // false
console.log(str.startsWith('传',0))//true
console.log(str.startsWith('传', 1)) // false
console.log(str.startsWith('传', 4)) // true
// 2. endsWith(检测字符,[字符串长度])
// 检测是否以某个字符结尾,返回布尔值
console.log(str.endsWith('知识')) // true
console.log(str.endsWith('传递')) // false
console.log(str.endsWith('播客', 4)) // true
console.log(str.endsWith('传递', 6)) // true
const str1 = "To be, or not to be, that is the question."
// 3. includes(搜索的字符,[检测位置])
// 判断一个字符串是否 包含 在另外一个字符串中,返回布尔值
console.log(str1.includes('to be')) // true
console.log(str1.includes('to be or')) // false
console.log(str1.includes('To be')) // true
console.log(str1.includes('To be', 1)) // false
console.log(str1.includes('To be',0)) //true
// 4. trim() 去除字符串首尾空格
const test = ' 123 456 '
console.log( test.trim() )// "123 456"
</script>
</body>
</html>