Typescript引用类型--字符串


一、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,'腹肌'))  // 西虹人瘦,燃烧我的腹肌


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM