2.03.01字符串API

2.03.01 字符串API

1.简单了解_proto_属性

  • 简单了解__proto__属性:
使用console.log(obj . __proto__)可以查看能被obj使用的属性和方法,只有构造函数构造出来的对象才能使用console.log(obj . __proto__)查看能被obj使用的属性和方法

2.手写快速排序

  1. 思想:
  • 把数组拆成三部分,一部分为变量middle,一部分为left数组,一部分为right数组,middle随便存一个数组中的一个数,我们这里存数组中的中间位置的元素
  • 遍历数组,数组中小于middle的值放在left中,把数组中大于middle的值放在right中,最后组合middle,left,right三部分
  • 然后我们用同样的方法排列left数组与right数组,最后直至left与right的数组长度为小于等于1
  • 最后我们就能得到一个升序的数列
  1. 代码:
    //手写快速排序:
    function quickSort(arr){

        //递归的结束条件
        if(arr.length<=1){
            return arr;
        }

        //随机找一个数组中的值都可以,我们这里找中间位置的值
        //Math.floor()实现一个非整数向下取整
        var  index=Math.floor(arr.length/2);
        var middle=arr.splice(index,1)[0];
        //建一个左数组,用于放比middle小的值
        var left=[];
        //建一个右数组,用于放比middle大或等的值
        var right=[];
        // 遍历参数数组,把数组中比middle小的值推进left
        //把数组中比middle大的值推进right
        arr.forEach(function(val){
            if(val<middle){
                left.push(val)
            }else{
                right.push(val)
            }
        });
        // 将left middle right 组合起来
        // 同时这是进行递归
        return quickSort(left).concat(middle,quickSort(right));
    } 

    var data = [50, 100, 10,20,30 ];
    var i = quickSort(data);
    console.log("data => " + data); // data => 50,100,20,30 //改变了原数组,把中间的元素切下来了
    console.log("快速排序过后的数组  =>  " + i); //快速排序过后的数组  =>  10,20,30,50,100

3.字符串API

  1. concat
  • 调用者是否改变:不会改变原字符串
  • 返回值:返回一个合并后的字符串
  • 参数:一个或多个字符串 .concat(str2, [, …strN])
  • 功能:方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
  • 语法:
    var s1 ="你好";
    var s2 = "世界";
    var res =s1.concat(s2);
    console.log(s1); //你好
    console.log(res); //你好世界
    var res =s1.concat(s2,"asdf",212);
    console.log(res); //你好世界asdf212
  1. split
  • 调用者是否改变: 不会改变原字符串
  • 返回值:返回一个数组
  • 参数:.split([separator[, limit]])
    • separator 指定表示每个拆分应发生的点的字符串。如果传递空字符串则拆分每个字母
    • limit 一个整数,限定返回的分割片段数量。
  • 功能:方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。
  • 语法:
    var st = "Hello World. How are you doing?";
    var newSt=st.split(" ");
    console.log("st => "+ st); //st => Hello World. How are you doing?
    console.log("newSt => "+ newSt); //newSt => Hello,World.,How,are,you,doing?
    console.log(newSt); // ['Hello', 'World.', 'How', 'are', 'you', 'doing?']

    var st = "张三,李四,赵五,孙六,钱七";
    var newSt=st.split();
    console.log(newSt);//['张三,李四,赵五,孙六,钱七'] 一个长度为1的数组
    
    var newSt=st.split("");
    console.log(newSt);//['张', '三', ',', '李', '四', ',', '赵', '五', ',', '孙', '六', ',', '钱', '七'] //每个字符都帮你切出来

    var newSt=st.split(",",2);
    console.log(st); //张三,李四,赵五,孙六,钱七
    console.log(newSt);//['张三', '李四']

  1. slice
  • 调用者是否改变:不会改变原字符串
  • 返回值:返回一个字符串
  • 参数:.slice(beginIndex[, endIndex])
    • beginIndex 从该索引(以 0 为基数)处开始提取原字符串中的字符。如果值为负数则倒数
    • endIndex 在该索引处结束前提取字符串(不包含次索引处字符),如果省略该参数,slice() 会一直提取到字符串末尾。可以为负
  • 功能:方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
  • 语法:
    // str1 的长度 length 是 23。
    var str1 = 'The morning is upon us.', 
        str2 = str1.slice(1, 8),
        str3 = str1.slice(4, -2),
        str4 = str1.slice(12),
        str5 = str1.slice(30);
        str6 = str1.slice(-1, -2);
    console.log(str1); // 输出The morning is upon us.
    console.log(str2); // 输出:he morn
    console.log(str3); // 输出:morning is upon u
    console.log(str4); // 输出:is upon us.
    console.log(str5); // 输出:"" 
    console.log(str6); // 输出:"" 
  1. indexOf
  • 注意:字符串也可以遍历
//字符串
    var str = "http://www.baidu.com";
    //字符串有长度length
    console. log(str.length);  // 20
    //循环字符串
    for(var i=0;i<str.length;i++){
        //打印每个字符
        console. log(str[i])
    }
  • 调用者是否会改变:不会改变原字符串
  • 返回值:返回整数 一个下标值
  • 参数 .indexOf(searchValue [, fromIndex])
    • searchValue 要被查找的字符串值。如果没有提供确切地提供字符串,searchValue 会被强制设置为 “undefined”
    • fromIndex 数字表示开始查找的位置。可以是任意整数,默认值为 0。
    • fromIndex 小于等于0时,等价于fromIndex=0。(与数组的API不一样,数组API中,索引值为负数,等价于与数组尾的偏移量)
  • 功能:.indexOf(searchValue [, fromIndex]) 方法返回调用它的 String 中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。
  • 语法:
    var st1="hello";
    var i=st1.indexOf();
    console.log("i => "+i);  //     i => -1  
    console.log("st1 => "+st1);//   st1 => hello 不改变原字符串

    var st1="undefined";
    var i=st1.indexOf();
    console.log("i => "+i);  //     i => 0   不传参数强制设置为字符串 "undefined"
    console.log("st1 => "+st1);//   st1 => undefined

    i=st1.indexOf("2");
    console.log("i => "+i); //i => -1

    i=st1.indexOf("e");
    console.log("i => "+i); //i => 3
    
    i=st1.indexOf("e",4);
    console.log("i => "+i); //i => 7

    i=st1.indexOf("e",1);
    console.log("i => "+i); //i => 1
  1. lastIndexOf
  • 调用者是否会改变:不改变原字符串
  • 返回值:返回整数 一个下标值
  • 参数 .lastIndexOf(searchValue[, fromIndex])
    • searchValue 一个字符串,表示被查找的值。如果searchValue是空字符串,则返回fromIndex。
    • fromIndex 待匹配字符串从str的第fromIndex位开始向左回向查找
    • fromIndex 小于等于0时,等价于fromIndex=0。(与数组的API不一样,数组API中,索引值为负数,等价于与数组尾的偏移量)
  • 功能: 方法返回调用 String 的指定值最后一次出现的索引,在一个字符串中的指定位置 fromIndex处从后向前搜索。如果没找到这个特定值则返回-1
  • 语法:
    var st1="hello";
    var i=st1.lastIndexOf();
    console.log("i => "+i);  //     i => -1  
    console.log("st1 => "+st1);//   st1 => hello 不改变原字符串

    var st1="undefined";
    var i=st1.lastIndexOf();
    console.log("i => "+i);  //     i => 0   不传参数强制设置为字符串 "undefined"
    console.log("st1 => "+st1);//   st1 => undefined

    i=st1.lastIndexOf("2");
    console.log("i => "+i); //i => -1

    i=st1.lastIndexOf("e");
    console.log("i => "+i); //i => 7
    
    i=st1.lastIndexOf("e",4);
    console.log("i => "+i); //i => 3

    i=st1.lastIndexOf("e",1);
    console.log("i => "+i); //i => 1

	//当fromIndex参数为负数时,等价于fromIndex=0
    st1="canal";
    i=st1.lastIndexOf("c", -4);
    console.log("i => " + i);   //i => 0
    i=st1.lastIndexOf("n", -4);
    console.log("i => " + i);   //i => -1
  1. substr
  • 调用者是否会改变:不会改变原字符串
  • 返回值:字符串
  • 参数 .substr(start[, length])
    • start 开始提取字符的位置。如果为负值,则倒数
    • length 提取的字符数。
  • 功能:方法返回一个字符串中从指定位置开始到指定字符数的字符。
  • 语法:
    var str = "abcdefghij";
    console.log("(1,2): "    + str.substr(1,2));   // (1,2): bc
    console.log("(-3,2): "   + str.substr(-3,2));  // (-3,2): hi
    console.log("(-3): "     + str.substr(-3));    // (-3): hij
    console.log("(1): "      + str.substr(1));     // (1): bcdefghij
    console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab
    console.log("(20, 2): "  + str.substr(20,2));  // (20, 2):
    console.log(str); //abcdefghij
  1. substring
  • 调用者是否会改变:不会改变原字符串
  • 返回值 字符串
  • 参数:.substring(indexStart[, indexEnd])
    • indexStart 需要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。
    • indexEnd 一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。若省略该属性substring 提取字符一直到字符串末尾。
    • 注意:indexStart 和 indexEnd 数小于 0 或为 NaN,则被当作 0。 任何小于0或大于 的参数值stringName.length都被分别视为0stringName.length
    • 如果indexStart大于 indexEnd,则 的效果substring()就像两个参数交换了一样;
  • 功能:方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。
  • 语法:
    var anyString = "Mozilla";

    // 输出 "Moz"
    console.log(anyString.substring(0,3));
    console.log(anyString.substring(3,0));
    console.log(anyString.substring(3,-3));
    console.log(anyString.substring(3,NaN));
    console.log(anyString.substring(-2,3));
    console.log(anyString.substring(NaN,3));

    console.log(anyString); //Mozilla

    // 输出 "lla"
    console.log(anyString.substring(4,7));
    console.log(anyString.substring(7,4));

    // 输出 ""
    console.log(anyString.substring(4,4));

    // 输出 "Mozill"
    console.log(anyString.substring(0,6));

    // 输出 "Mozilla"
    console.log(anyString.substring(0,7));
    console.log(anyString.substring(0,10));
  1. replace
  • 调用者是否会改变:不会改变原字符串
  • 返回值 返回一个字符串
  • 参数 .replace(substr, newSubStr|function)
    • substr 一个将被 newSubStr 替换的字符串。
    • newSubStr 用于替换掉第一个参数在原字符串中的匹配部分的字符串。
    • function 一个用来创建新子字符串的函数,该函数的返回值将替换掉第一个参数匹配到的结果。
  • 功能:方法返回一个由替换值替换部分substr模式匹配项后的新字符串。
  • 语法:
    var re = "apples";
    var str = "apples are round, and apples are juicy.";
    var newstr = str.replace(re, "oranges"); 
    console.log(newstr);    // oranges are round, and apples are juicy.
    console.log(str);   //apples are round, and apples are juicy.

    var re = "apples";
    var str = "apples are round, and apples are juicy.";
    // 给指定单词加引号
    var newstr = str.replace(re, function(match) {
        // match 是匹配到的子串。
        return '"' + match + '"' 
    }) 
    console.log(newstr);    // "apples" are round, and apples are juicy.
  1. replaceAll
  • 调用者是否会改变:不会改变原字符串
  • 返回值 返回一个字符串
  • 参数 str.replaceAll(substr, newSubstr|function) 参考replace
  • 功能:方法返回一个新字符串,新字符串所有满足 substr 的部分都已被 newSubstr 或 function 返回值替换。
  • 语法:
    var re = "apples";
    var str = "apples are round, and apples are juicy.";
    var newstr = str.replaceAll(re, "oranges");
    console.log(newstr);    //oranges are round, and oranges are juicy.
    console.log(str);       //apples are round, and apples are juicy.

    var re = "apples";
    var str = "apples are round, and apples are juicy.";
    // 给指定单词加引号
    var newstr = str.replaceAll(re, function(match) {
        // match 是匹配到的子串。
        return '"' + match + '"' 
    })
    console.log(newstr);  //"apples" are round, and "apples" are juicy.
  1. toUpperCase
  • 调用者是否会改变:不会改变原字符串
  • 返回值 字符串
  • 参数 无
  • 功能:把字母字符转大写
  • 语法:
    var str = "AbCdsdfe";
    var st=str.toUpperCase();
    // 把字母字符转大写
    console.log(st);    //ABCDSDFE
    console.log(str);   //AbCdsdfe
  1. toLowerCase
  • 调用者是否会改变:不会改变原字符串
  • 返回值 一个字符串
  • 参数 无
  • 功能:会将调用该方法的字符串值转为小写形式,并返回。
  • 语法:
        // 把字母字符转小写
        var str1 = "AbCDDS";
        var st1=str1.toLowerCase();
        console.log(st1);    //abcdds
        console.log(str1);   //AbCDDS
  1. charAt
  • 调用者是否会改变:不会改变原字符串
  • 返回值 一个字符
  • 参数 .charAt(index) index 一个介于0 和字符串长度减1之间的整数。默认值为0
  • 功能:方法从一个字符串中返回指定的字符。
  • 语法:
    var anyString = "Brave new world";
    console.log("The character at index 0   is '" + anyString.charAt(0)   + "'"); // The character at index 0 is 'B'
    console.log("The character at index 1   is '" + anyString.charAt(1)   + "'"); // The character at index 1 is 'r'
    console.log("The character at index 2   is '" + anyString.charAt(2)   + "'"); // The character at index 2 is 'a'
    console.log("The character at index 3   is '" + anyString.charAt(3)   + "'"); // The character at index 3 is 'v'
    console.log("The character at index 4   is '" + anyString.charAt(4)   + "'"); // The character at index 4 is 'e'
    console.log("The character at index 999 is '" + anyString.charAt(999) + "'"); // The character at index 999 is ''
    console.log(anyString); //Brave new world
  1. charCodeAt
  • 调用者是否会改变:不会改变原字符串
  • 返回值 一个数字 number类型
  • 参数 .charCodeAt(index) index 一个大于等于 0,小于字符串长度的整数。如果不是一个数值,则默认为 0。
  • 功能:指定 index 处字符的 UTF-16 代码单元值的一个数字(小于 65,536 的值);如果 index 超出范围,charCodeAt() 返回 NaN。
  • 语法:
        var str="ABC";
        var box=str.charCodeAt(0);
        console.log(str); //ABC
        console.log(box);  //65
        console.log(typeof box); //number
  1. repeat
  • 调用者是否会改变:不会改变原字符串
  • 返回值 一个字符串
  • 参数 .repeat(count) 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。
  • 功能:构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。
  • 语法:
        var str="ABC";
        var box=str.repeat(2);

        console.log(str);  //ABC
        console.log(box);   //ABCABC
        console.log(typeof box);  //string

        "abc".repeat(-1)     // RangeError: repeat count must be positive and less than inifinity
        "abc".repeat(0)      // ""
        "abc".repeat(1)      // "abc"
        "abc".repeat(2)      // "abcabc"
        "abc".repeat(3.5)    // "abcabcabc" 参数count将会被自动转换成整数.
        "abc".repeat(1/0)    // RangeError: repeat count 必须大于等于 0 且 小于+inifinity

  1. trim
  • 调用者是否会改变:不会改变原字符串
  • 返回值 一个字符串
  • 参数:无
  • 功能:方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等)
  • 语法:
        var orig1 = '   foo  ';
        var box = orig1.trim();
        console.log(box);           // 'foo'
        console.log(typeof box);    //string
        console.log(orig1);         //   '   foo '

        var orig1 = 'foo    ';
        var box = orig1.trim();
        console.log(box);           // 'foo'
        console.log(typeof box);    //string
        console.log(orig1);         //'foo    '
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值