zoukankan      html  css  js  c++  java
  • JavaScript String 对象

    /*
     * 基本类型 string 和包装类型 String
     * 都可以调用系统内置的方法 但是基本类型不能为自己添加属性和方法 包装类型可以为自己添加属性和方法
     */
    // 定义基本类型 string
    var box = "lee";
    // 调用内置方法 substring()
    box.substring(); // "lee"
    // 添加属性和方法
    box.name = 'zhang';
    box.getName = function() {
        return box.name;
    }
    box.name; // undefined
    box.getName(); // Uncaught TypeError: box.getName is not a function(…)
    
    // 定义包装类型 String
    var box = new String("lee");
    // 调用内置方法 substring()
    box.substring(); // "lee"
    // 添加属性和方法
    box.name = 'zhang';
    box.getName = function() {
        return box.name;
    }
    box.name; // "zhang"
    box.getName(); // "zhang"
    
    // String对象常用api
    var box = "Lee";
    // constructor属性返回String的构造函数
    box.constructor; // function String() { [native code] }
    // charAt() 返回在指定位置的字符。 
    box.charAt(0); // "L"
    // charCodeAt() 返回在指定的位置的字符的 Unicode 编码。 
    box.charCodeAt(0); // 76
    box.charCodeAt(1); // 101
    // fromCharCode() 从字符编码创建一个字符串。 
    String.fromCharCode(76, 101, 101); // "Lee"
    // concat() 连接字符串。 
    box.concat(" is", " a", " teacher"); // "Lee is a teacher"
    // toLowerCase() 把字符串转换为小写。 
    box.toLowerCase(); // "lee"
    // toUpperCase() 把字符串转换为大写。 
    box.toUpperCase(); // "LEE"
    // toLocaleLowerCase() 按照本地方式把字符串转换为小写。 
    box.toLocaleLowerCase(); // "lee"
    // toLocaleUpperCase() 按照本地方式把字符串转换为大写。 
    box.toLocaleUpperCase(); // "LEE"
    // match() 找到一个或多个正则表达式或者字符串的匹配。 
    box.match("L"); // ["L"]
    box.match(/L/); // ["L"]
    box.match("O"); // null
    // search() 检索与正则表达式相匹配的值。 
    box.search("L"); // 0
    box.search("O"); // -1
    // replace() 替换与正则表达式或指定字符串匹配的子串。 
    box.replace("L", "Q"); // "Qee"
    box.replace(/L/, "Q"); // "Qee"
    // bold() 使用粗体显示字符串。
    box.bold(); // "<b>Lee</b>"
    // link() 将字符串显示为链接。
    "baidu".link("http://www.baidu.com"); // "<a href="http://www.baidu.com">baidu</a>"
    
    /*
     * slice(start, end) 提取字符串的片断,并在新的字符串中返回被提取的部分。
     * start 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。
     * end 要抽取的片段的结尾的下标。若未指定此参数,则默认截取到原字符串结尾。如果该参数是负数,那么它规定的是从字符串的尾部开始算起的位置。
     * end 小于 start 返回空
     */
    var box = "LeeLeeLee";
    box.slice(1, 2); // "e"
    box.slice( - 2); // "ee"
    box.slice(2, -2); // "eLeeL"
    box.slice( - 2, -1); // "e"
    box.slice(2, 1); // ""
    
    /*
     * substring(beginIndex, endIndex) 提取字符串中两个指定的索引号beginIndex和endIndex之间的字符
     * beginIndex可选参数 默认从0开始 endIndex可选参数 默认字符串长度
     * 如果 beginIndex 比 endIndex 大 那么该方法在提取子串之前会先交换这两个参数
     * substring() 和 slice() 的区别
     * slice()更灵活可以接收负数做参数
     * substring()不能接收负数做参数 如果参数为负数 直接转为0
     */
    var box = "lee";
    box.substring(0, 3); // "lee"
    box.substring(); // "lee"
    box.substring(3, 0); // "lee"
    box.substring( - 2); // "lee"
    box.substring(2, -1); // "le"
    
    /*
     * substr(start, length) 从start起始索引号提取字符串中length指定数目的字符
     * start如果是非负数,那么该参数声明从字符串的头部开始算起的位置,0为第一个字符。
     * start如果是负数,那么该参数声明从字符串的尾部开始算起的位置,-1为最后一个字符。
     * length如果不写默认从start起始索引号提取到字符串最后一位。
     * length如果是负数,直接转为0,返回空。
     */
    var box = "lee";
    box.substr(1, 2); // "ee"
    box.substr( - 2); // "ee"
    box.substr(2, -1); // ""
    
    /*
     * indexOf(searchvalue, fromindex) 从fromindex向后搜索字符串searchvalue
     * fromindex不写默认为零 即 从开始位置搜索
     * 返回首次搜索到的位置 找不到返回-1
     */
    var box = "LLLEEE";
    box.indexOf('O'); // -1
    box.indexOf('L'); // 0
    box.indexOf('L', 2); // 2
    
    /*
     * lastIndexOf(searchvalue, fromindex)
     * 从fromindex向前搜索字符串searchvalue 返回首次搜索到的位置
     */
    var box = "LLLEEE";
    box.lastIndexOf('L', 3); // 2
    
    /* stringObject.localeCompare(target) 用本地特定的顺序来比较两个字符串。
     * 如果 stringObject 小于 target,则该方法返回 -1。
     * 如果 stringObject 大于 target,则该方法返回 1。
     * 如果两个字符串相等,该方法返回 0。
     */
    "lee".localeCompare("lee"); // 0
    "leh".localeCompare("lee"); // 1
    "led".localeCompare("lee"); // -1
  • 相关阅读:
    2021.07.14牛客学习
    2021.07.13学习总结
    new和malloc区别(自己口头描述)以及delete用法
    排序整理(c++实现),搭配图解
    如何将bilibili上缓存的文件转成MP4
    第07组 团队Git现场编程实战
    第二次结队编程作业
    团队项目-需求分析报告
    团队项目-选题报告
    第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/pumushan/p/6732353.html
Copyright © 2011-2022 走看看