• [Typescript]基础篇之 String 对象


    String 对象简介

    String 对象是用于处理文本(字符串)

    与 string 区别

    string 是基础数据类型;
    String 是对象;

    String

    定义

    let txt = new String(value);
    //简单方式
    let txt = "string";
    

    属性

    属性说明
    length返回字符串的长度
    prototype允许向对象添加属性和方法
    constructor创建该对象的函数的引用

    方法

    方法说明
    charAt返回在指定位置的字符
    charCodeAt返回指定位置字符的 Unicode 编码
    concat连接两个或更多字符串,并返回新的字符串
    indexOf返回指定字符串值在字符串中首次出现位置
    lastIndexOf搜索方向从后向前搜索最后出现字符串位置值,返回位置数值从前向后数
    localeCompare用本地特定的顺序来比较两个字符串
    match查找找到一个或多个正则表达式的匹配
    replace替换与正则表达式匹配的子串
    search检索与正则表达式相匹配的值
    slice提取字符串的片断,并在新的字符串中返回被提取的部分
    split把字符串分割为子字符串数组
    substr返回从起始索引号开始指定长度的字符
    substring提取字符串中指定索引号之间的字符
    toLocaleLowerCase根据主机语言环境转换为小写
    toLocaleUpperCase根据主机语言环境转换为大写
    toLowerCase把字符串转换为小写
    toString返回字符串
    toUpperCase把字符串转换为大写
    valueOf返回指定字符串对象的原始值

    属性的使用

    1. constructor
    let str = new String( "This is string" );
    console.log("str.constructor is",str.constructor)
    //str.constructor is:function String() { [native code] }
    console.log(str.constructor)
    //ƒ String() { [native code] }
    
    1. prototype
    function employee(id:number,name:string) {
        this.id = id
        this.name = name
    }
    
    let emp = new employee(123,"admin")
    employee.prototype.email = "admin@runoob.com"
    
    console.log("员工邮箱: "+emp.email)
    

    方法的使用

    lastIndexOf

    搜索方向从后向前,搜索字符串最后的位置值,返回位置值从前向后数

    let str1 = new String( "This is string one and again string" );
    let index = str1.lastIndexOf( "string" );
    console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 29
    
    index = str1.lastIndexOf( "one" );
    console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 15
    

    localeCompare

    用本地特定的顺序来比较两个字符串

    let str1 = new String( "This is beautiful string" );
    
    let index = str1.localeCompare( "This is beautiful string");
    // 0
    console.log("localeCompare first :" + index );
    

    replace

    替换与正则表达式匹配的子串

    let re = /(\w+)\s(\w+)/;
    let str = "zara ali";
    let newstr = str.replace(re, "$2, $1");
    console.log(newstr); // ali, zara
    

    search

    检索与正则表达式相匹配的值,未找到返回-1

    let re1 = /apples/gi;
    let re2 = /apples/g;
    let re3 = /apples/;
    let str = "Apples are round, apples are red, and apples are juicy.";
    if (str.search(re) == -1 ) {
       console.log("Does not contain Apples" );
    } else {
       console.log("Contains Apples", re1);//4
       console.log("Contains Apples", re2);//22
       console.log("Contains Apples", re3);//22
    }
    
    • /gi 搜索第一次检索到正则的位置值,不区分大小写
    • /g 搜索第一次检索到正则的位置值,区分大小写
    • / 搜索第一次检索到正则的位置值,区分大小写

    slice

    提取字符串的片断,并在新的字符串中返回被提取的部分

    let re = 'apples';
    let str = "Apples are round, apples are red, and apples are juicy.";
    let newad=str.slice(re)
    str="new word"
    console.log("log1:" ,str)//"log1:new word"
    console.log("log2:" ,newad)//"log2:Apples are round, apples are red, and apples are juicy."
    

    返回的是新地址的字符串,因此修改该值不会影响原有字符串值

    split

    字符串分割为子字符串数组。

    string.split(splitChar, length)
    
    • splitChar 用于分割的字符串
    • string 被分割字符串
    • length 分割字符串后返回的字符串数组的长度
    let str = "Apples are round, and apples are juicy.";
    let splitted = str.split(" ", 3);
    console.log(splitted)  // [ 'Apples', 'are', 'round,' ]
    

    substring

    截取字符串中指定的两个索引号之间的字符

    string.substring(start, end?)
    
    • start 截取字符串开始位置索引,若是没有 end,默认为截取到最后一个字符串
    • end 截取字符串结束位置索引
    let str = "RUNOOB GOOGLE TAOBAO FACEBOOK";
    console.log("(1,2): "    + str.substring(1,2));   // U
    console.log("(0,10): "   + str.substring(0, 10)); // RUNOOB GOO
    console.log("(5): "      + str.substring(5));     // B GOOGLE TAOBAO FACEBOOK
    

    toLocaleLowerCase

    根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。

    let str = "Runoob Google";
    console.log(str.toLocaleLowerCase( ));  // runoob google
    

    toLocaleUpperCase()

    据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。

    let str = "Runoob Google";
    console.log(str.toLocaleUpperCase( ));  // RUNOOB GOOGLE
    

    valueOf()

    返回指定字符串对象的原始值。

    let str = new String("Runoob");
    console.log(str.valueOf( ));  // Runoob
    
  • 相关阅读:
    React 网络请求
    【unity实战】Unity实现2D人物双击疾跑
    再服务器上配置其他版本的DGL
    【计算机网络】第三章·数据链路层 超硬核复习好物,考前必看!!!
    JSON Web Token
    STM32和ESP32哪个更适合初学者
    vue项目运行出现66% buil 98% after emitting CopyPlugin
    动态规划基础篇(LeetCode每日一题计划)
    C Primer Plus(6) 中文版 第7章 C控制语句:分支和跳转 7.4 一个统计单词的程序
    elementplus如何实现dialog遮罩层外的元素可以被操作点击
  • 原文地址:https://blog.csdn.net/tjj3027/article/details/126857601