字符串常用方法:
charAt() charCodeAt() concat() endsWith() / startsWith() includes()
indexOf() replace() slice() / substring () split() trim()
toLowerCase() toUpperCase()
1、charAt()
它用于返回指定位置处的字符,字符串中第一个字符的位置为0
const str = 'ch112in@a0'
console.log(str.charAt(1)) // 'h'
console.log(str.charAt(str.length - 1)) // '0'
2、charCodeAt()
用于检索字符串中特定位置的字符的Unicode 值 ,'a'->97 'z'->122
const str = 'ch112in@a0'
console.log(str.charCodeAt(str.length - 2)) // 97
charCodeAt()练习:统计字符串中字母数量
const str = 'ch112in@a0'
let num = 0
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
num++
}
}
console.log(`字符串:${str}中字母个数有:${num}个`)
3、concat()
将字符串参数连接到调用的字符串,并返回一个新的字符串
const str1 = 'Hello'
const str2 = 'World'
console.log(str1.concat(' ', str2)) // Hello World
console.log(str2.concat(' ', str1)) // World Hello
4、endsWith()
用于判断一个字符串是否以指定子字符串结尾,如果是返回true,如果不是返回false
const str3 = 'Cats are the best!' // 共18个字符
console.log(str3.endsWith('best!')) // true
console.log(str3.endsWith('best')) // false
console.log(str3.endsWith('best', 17)) //true
console.log(str3.endsWith('best!', 15)) //false
5、startsWith()
用于判断一个字符串是否以指定子字符串开头,如果是返回true,如果不是返回false
const str3 = 'Cats are the best!'
console.log(str3.startsWith('Cat')) //true
console.log(str3.startsWith('Cat', 1)) //false
6、includes()
方法执行区分大小写的搜索,以确定是否在一个字符串中找到另一个字符串,并根据情况返回 true 或 false
const str4 = 'The quick brown fox jumps over the lazy dog.'
console.log(`the word 'fox' ${str4.includes('fox') ? 'is' : 'is not'} in the str4`)
7、indexOf()
字符串中搜索指定子字符串,并返回其第一次出现的位置索引
const paragraph = "I think Ruth's dog is cuter than your dog!"
const searchTerm = 'dog'
const indexOfFirst = paragraph.indexOf(searchTerm) // 15
const indexOfSecond=paragraph.indexOf(searchTerm,indexOfFirst+1) // 38
console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`)
console.log(`The index of the seconde "${searchTerm}" is ${indexOfSecond}`)
8、replace()
返回一个新的字符串,其中一个、多个或全部 子字符串 换成特定的 字符串
const paragraph = "I think Ruth's dog is cuter than your dog!"
console.log(paragraph.replace("Ruth's",'my')) // I think my dog is cuter than your dog!
9、slice()
提取字符串的一部分,并将其作为新的字符串返回,而不修改原始字符串
const str5='The quick brown fox jumps over the lazy dog.'
console.log(str5.slice(4)) //quick brown fox jumps over the lazy dog.
console.log(str5.slice(4,19)) // quick brown fox [4,19)
console.log(str5.slice(-4)) // dog.
10、substring()
返回字符串从起始索引到结束索引(不包括)的部分,如果没有结束索引,则返回到字符串末尾
const str6='Mozilla'
console.log(str6.substring(2,5)) // zil
console.log(str6.substring(2)) // zilla
11、split()
通过搜索模式将字符串分割成一个有序的子串列表,将子串放入一个数组,并返回该数组
const str5='The quick brown fox jumps over the lazy dog.'
console.log(str5.split(' '))
//["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]
console.log(str5.split(''))
12、trim()
从字符串的两端移除空白字符,并返回一个新的字符串,且不改变原始字符串
const str7=' hello wold! '
console.log(str7.trim()) // hello wold!
13、toLowerCase()
将字符串转换为小写形式
const str8='HELLO WOLD!'
console.log(str8.toLowerCase()) //hello wold!
14、toUpperCase()
将字符串转换为大写形式
const str9='hello wold!'
console.log(str9.toUpperCase()) //HELLO WOLD!