首页 > 其他 > 详细

字符串相关方法

时间:2021-05-04 17:44:25      阅读:26      评论:0      收藏:0      [点我收藏+]
    // var str = "Hello Eric";

    // length  获取字符串长度
    // console.log(str.length); // 10

    // charAt  返回字符串中指定位置的字符
    // var result = str.charAt(0)  // 类似 str[0]
    // console.log(result);  // H


      // charCodeAt  返回字符串中指定位置 unicode 编码
      // var result = str.charCodeAt(0)  
      // console.log(result);   // 72


      // fromCharCode()  可以根据编码去获取字符串
      // console.log(String.fromCharCode(72)); // H


      // concat()  连接两个 或多个 字符串  // 作用和 + 效果一样
      // var  result = str.concat(‘你好‘,‘再见‘) 
      // console.log(result);  // Hello Eric你好再见


      // indexOf()  可以检索一个字符串是否含有 指定内容
      //  如果字符串含有该内容 则会返回第一次出现的索引 
      // 如果没有找到 则返回 -1
      // 可以指定 一个 第二个参数 指定开始查找的位置
      // lastIndexOf() 从后往前找

     //  var str = "Hello Eric Ha ha";
      // console.log(str.indexOf(‘e‘)); // 1
      // console.log(str.indexOf(‘E‘)); // 6 
      // console.log(str.indexOf(‘a‘)); // -1
      // console.log(str.indexOf(‘H‘,2)); // 11

      // result = str.lastIndexOf(‘H‘)
      // result2 = str.lastIndexOf(‘H‘,5)
      // console.log(result); // 11
      // console.log(result2); //0


      /**
       *  slice()
       *    可以从字符串中截取指定的内容
       *    不会影响原字符串 而是将截取到内容返回
       *    参数
       *      第一个: 开始位置的索引 (包括开始位置)
       *      第二个: 结束位置的索引 (不包括结束位置)
       *            如果省略第二个参数 则会截取后面所有的
       *      也可以传递一个负数作为参数 负数的话 将会从后边计算 
      */
      // var str = "abcdefghijklmn";
      // result = str.slice(0,2) 
      // console.log(result); // ab

      // result = str.slice(1) 
      // console.log(result); // bcdefghijklmn

      // result = str.slice(-1) 
      // console.log(result); // n

      // result = str.slice(1,-1) 
      // console.log(result); // bcdefghijklm


      /**
       * substring()
       *   可以用来截取一个字符串, 和 slice() 类似
       *    参数
       *      第一个: 开始位置的索引 (包括开始位置)
       *      第二个: 结束位置的索引 (不包括结束位置)
       *     不同的是 这个方法不能接受负值 作为参数
       * 
       *        如果传递一个负值,则默认使用 0 
       *        而且他还自动调整参数的位置 如果第二个参数小于 第一个 则自动交换位置
      */

      // result = str.substring(0,2)
      // console.log(result); // ab

      /**
       * substr()
       *  用来截取字符串
       *  参数 
       *    1 截取开始位置的索引
       *    2 截取的长度
      */
      // var str = "abcdefghijklmn";
      // result = str.substr(3,2)
      // console.log(result);  // de


      /**
       * split()
       *  可以将一个字符串拆分为一个数组
       *  参数
       *    需要一个字符串 作为参数,将会根据 字符串去拆分数组
      */
      // var str = ‘abc,bcd,efg,hij‘;
      // result = str.split(‘,‘)
      // console.log(result);  // ["abc", "bcd", "efg", "hij"]


      /**
       * toUpperCase()
       *  将字符串转行为大写 
      */
      // str = ‘adcdefg‘;
      // result = str.toUpperCase();
      // console.log(result);  // ADCDEFG


       /**
       * toLowerCase()
       *  将字符串转行为小写 
      */
      str = ‘ADCDEFG‘;
      result = str.toLowerCase();
      console.log(result);  // adcdefg

字符串相关方法

原文:https://www.cnblogs.com/eric-share/p/14729653.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!