export class TemplateFormsComponent implements OnInit {
constructor() { }
ngOnInit() {
this.test()
}
testStringF:string="字符串AaaBb"
testStringS: string = "字符串CccDd"
testStringT: string = " 字符串EeFf "
testStringU: number = 97
arr:any[]=['1','2','3']
test(){
//charAt() 返回指定索引位置的字符
console.log(this.testStringF.charAt(0)) // 1
//charCodeAt() 返回指定索引位置字符的 Unicode 值
console.log(this.testStringF.charCodeAt(4))// 2
//concat() 连接两个或多个字符串,返回连接后的字符串
console.log(this.testStringF.concat(this.testStringS))// 3
//endsWith() 判断当前字符串是否是以指定的子字符串结尾的(区分大小写)
console.log(this.testStringF.endsWith("b"))// 4
//fromCharCode() 将 Unicode 转换为字符串
console.log(String.fromCharCode(this.testStringU))// 5
//indexOf() 返回某个指定的字符串值在字符串中首次出现的位置
console.log(this.testStringF.indexOf("a"))// 6
//includes() 查找字符串中是否包含指定的子字符串
console.log(this.testStringF.includes("a"))// 7
//lastIndexOf() 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置
console.log(this.testStringF.lastIndexOf("a"))// 8
//match() 查找找到一个或多个正则表达式的匹配
console.log(this.testStringF.match(/a/g))// 9
//replace() 在字符串中查找匹配的子串,并替换想要替换的字符串
console.log(this.testStringF.replace("a", 'E'))// 10 //第一个参数可以写正则表达式
console.log(this.testStringF.replace("a", ''))// 10-1 //可用做删除第一个查找到的匹配字串
//replaceAll() 在字符串中查找匹配的子串,并替换与正则表达式匹配的所有子串
//console.log(this.testStringF.replaceAll("a", 'E')) 用于替换所有查找字符串,部分浏览器不兼容,慎用
//search() 用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,查找并返回指定字符串第一次出现的位置。如果没有找到任何匹配的子串,则返回-1。
//功能与indexOf类似,但indexOf参数不能传入正则表达式,如果单纯查找字符串,indexOf效率更高。
console.log(this.testStringF.search("符"))// 11
//slice() 截取字符串的片断,传入两个参数,第一个表示截取开始位置,第二个表示结束位置
//如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。
console.log(this.testStringF.slice(1, 3))// 12
//split() 把字符串分割为字符串数组,如果多个相同的被分割的字符串连在一起,会产生空字符元素
console.log(this.testStringF.split("a"))// 13
//或者头或尾元素被分割,也会产生空字符元素
console.log(this.testStringF.split("字"))// 13-1
console.log(this.testStringF.split("b"))// 13-2
//startsWith() 查看字符串是否以指定的子字符串开头,返回boolean值
console.log(this.testStringF.startsWith("b"))// 14
//substr() 提取字符串,传入两个参数,第一个表示截取开始位置,第二个表示提取的长度
console.log(this.testStringF.substr(1, 3))// 15
//substring() 方法用于提取字符串中介于两个指定下标之间的字符,与slice()的区别是,不可传负值
console.log(this.testStringF.substring(1, 3))// 16
//toLowerCase() 把字符串转换为小写
console.log(this.testStringF.toLowerCase())// 17
//toString() 返回字符串对象值
console.log(this.arr.toString())// 18
//trim() 去除字符串两边的空白
console.log(this.testStringT.trim())// 19
//includes() 查找字符串中是否包含指定的子字符串,返回boolean值
console.log(this.testStringF.includes("a"))// 20
//repeat() 复制字符串指定次数,并将它们连接在一起返回
console.log(this.testStringF.repeat(2))//21
}
}
效果