一、Typescript中字符串的兩種類型:
1、基本類型字符串:由單引號或者雙引號'包裹的'一串字符;
2、引用類型字符串:由new實例化的String類型。
二、基本類型的字符串可以直接使用引用類型的屬性和方法
let Jude: string = 'YQ'
let JudeYQ: String = new String('JudeYQ')
console.log(Jude) // YQ
console.log(JudeYQ) // [String:'JudeYQ']
// 基本類型的字符串可以直接使用引用類型的屬性和方法
console.log(Jude.length) // 2
console.log(JudeYQ.length) // 6
三、字符串常用的方法
1、字符串查找 indexOf()和lastIndexOf(),二者返回的都是字符串的下標。
let word:string = '西虹人瘦,燃燒我的卡路里'
let Calorie: string = '卡路里'
console.log(word.indexOf(Calorie)) // 9
let ST:string = '沈騰'
console.log(word.indexOf(ST)) // -1 沒有查找到返回-1
console.log(word.lastIndexOf(Calorie)) // 9 從字符串尾部開始查找字符串的位置 和indexOf()返回的都是字符串下標
2、字符串的截取, substring()
// 字符串的截取
console.log(word.substring(9)) // 卡路里
console.log(word.substring(9,12)) // 卡路里
3、字符串的替換,replace()
console.log(word.replace(Calorie,'腹肌')) // 西虹人瘦,燃燒我的腹肌
