javascript 字符串

前言:这是笔者学习之后自己的理解与整理。如果有错误或者疑问的地方,请大家指正,我会持续更新!
  字符串可以使用单引号和双引号,两者没有区别,但要注意起始和终止要一致。
  字符串转换有两种方式:obj.toString()String(obj)转型函数。nullundefined没有toString()方法,任何类型都可以使用String(obj)转型函数。
  需注意:其中数组转换字符串时会保留逗号,所以数组转换字符串一般使用arr.join()方法,后面数组方法讲解

  var arr = ['abc', 123, '阿f西4吧'];

  console.log(String(arr)); //abc,123,阿f西4吧
  console.log(arr.toString()); //abc,123,阿f西4吧

  console.log(arr.join()); //abc,123,阿f西4吧
  console.log(arr.join('')); //abc123阿f西4吧
  console.log(arr.join('+')); //abc+123+阿f西4吧

字符串截取

str.substring(start,end)

  • 字符串截取。start为开始截取的位置的下标(从0开始),end为结束截取的位置(不包含结束位)。
  • 它们俩顺序可以颠倒,引擎会自己判断,但不支持负数值,会当做0处理。
  • end可以不写,这样会截取到字符串最后。
  var str = 'hello world!';
  //end没设置,就截取到末尾
  console.log(str.substring(4));//o world!
  //不支持负数值,当做0处理
  console.log(str.substring(-4));//hello world!
  //支持颠倒顺序,引擎会判断大小,注意末尾不包含
  console.log(str.substring(2,2));//  ,因为不包含结尾,所以这里什么都没有
  console.log(str.substring(2,5));//llo
  console.log(str.substring(5,2));//llo

str.substr(start,length)

  • 字符串截取。start为开始截取的位置的下标(从0开始),length为指定数目。
  • length可以不写,这样会截取到字符串最后。
  • 如果start为负数,则表示从字符串尾部开始算起。
  let str = 'www.baidu.com'
  console.log(str.substr(1, 3)) // ww.
  console.log(str.substr(0)) // www.baidu.com
  console.log(str.substr(-3, 3)) // com
  console.log(str.substr(-1, 5)) // m  (目标长度较大的话,以实际截取的长度为准)

str.slice(start,end)

  • 字符串截取。start为开始截取的位置的下标(从0开始),end为结束截取的位置(不包含结束位)。
  • substring不同的是可以设置负值,这样就从字符串结尾处开始算(-1是最后一个字符,-2倒数第2个),但不支持颠倒顺序;end可以不写,这样会截取到字符串最后;
  var str = 'hello world!';

  //end没设置,就截取到末尾
  console.log(str.slice(4));//o world!
  //不支持支持颠倒顺序,注意末尾不包含
  console.log(str.slice(2,2));//  ,因为不包含结尾,所以这里什么都没有
  console.log(str.slice(2,5));//llo
  console.log(str.slice(5,2));//  ,因为不支持颠倒顺序,所以什么也没有,也不报错
  //支持负数值
  console.log(str.slice(-2));//d!
  console.log(str.slice(-2,5));//  ,因为不支持颠倒顺序,所以什么也没有,也不报错
  console.log(str.slice(-5,-2));//orl
  console.log(str.slice(-2,-5));//  ,因为不支持颠倒顺序,所以什么也没有,也不报错

str.replace(change,new)

  • 在原字符串中用某些字符替换另一些字符。
  • 找到第一个符合的字符就直接替换,然后返回,后面的就不查了。
  • 如果没找到需替换的字符,也不会报错,直接返回原数据。
  var str = 'hello world!';
  console.log(str.replace('hello','nihao'));//nihao world!

  //如果没找到,直接返回原数据
  console.log(str.replace('axiba','sss'));//hello world!
  
  //只转换找到的第一个o
  console.log(str.replace('o','S'));//nihaS world!
  
  //全局搜索o,并且全部转换
  //用正则表达式写,引号可以用 / 代替引号
  console.log(str.replace(/o/g,'S'));//nihaS wSrld!
  
  //不区分大小写,用/i
  var test = 'java Javascript JAVA';
  console.log(test.replace(/JAva/i,'nice'));//nice Javascript JAVA
  
  //全局不区分大小写,用/ig
  console.log(test.replace(/JAva/ig,'nice'));//nice nicescript nice

字符串转换

str.split(分隔符,howmany)

  • 字符串转换为数组。howmany为可选参数,设置返回数组的最大长度(可能小于它),如果不写,整个字符串都会被分割。
  • 返回一个数组,并且原字符串不会发生改变。
  var str = 'hello world!';
  //没写引号,或者分隔符找不到,就转换为1个整的数据
  console.log(str.split());//['hello world!']
  console.log(str.split('ss'));//['hello world!']
  console.log(str);//hello world!
  
  //有引号,但没有分隔符(注意连空格都没有),数组默认拆散全部字符串;
  console.log(str.split(''));//['h','e','l','l','o','','w','o','r','l','d','!']
  console.log(str.split('',3));//['h','e','l']
  console.log(str);//hello world!
  
  //用空格做分隔符
  console.log(str.split(' '));//['hello','world']
  console.log(str.split(' ',3));//['hello','world']
  console.log(str);//hello world!
  
  //用字母o做分隔符
  console.log(str.split('o'));//['hell',' w','rld']
  console.log(str.split('o',2));//['hell',' w']
  console.log(str);//hello world!

str.trim()

  • 删除字符串的头尾空格。
  • 不会改变原始字符串。
  var str = '  hello ';
  console.log(str.trim()); //hello

str.toLowerCase() 和 str.toUpperCase()

  • 把字符串转换为小写。
  • 把字符串转换为大写。
  • 返回新字符串,原字符串不变。
  var str = 'HEllo';
  console.log(str.toLowerCase()); //hello
  console.log(str); //HEllo

  console.log(str.toUpperCase()); //HELLO
  console.log(str); //HEllo

一串英语字符串首字母大写

  • 可以设置css属性,text-transform:Capitalize;
  function titleCase(str) {
    var newarr = str.toLowerCase().split(" ");
    var tempArr = newarr.map(i => {
      return i.replace(i.charAt(0), i.charAt(0).toUpperCase());
    })
    return tempArr.join(" ");
  }
  console.log(titleCase("I'm a javascript coder"))//I'm A Javascript Coder

字符串查找

str.index0f(find,from) 和 str.lastIndexOf(find,from)

  • 定位字符串中某指定字符首次出现的位置,如果没找到返回-1
  • from是从哪个位置开始找;如果没有就是默认从0开始。
  • 字符串的索引从零开始, 所以字符串第一字符为[0],第二个字符为[1], 递增。
  • str.lastIndexOf("find")从末尾开始查指定字符首次出现的位置(正向位置,不是指定字符末尾)。
  var str = 'hello world!';
  console.log(str.indexOf('llo'));//2
  console.log(str.indexOf('o'));//4
  console.log(str.indexOf('o',5));//7
  console.log(str.indexOf('o',8));//-1
  
  console.log(str.indexOf('sss'));//-1
  //非0为true;0为false; 所以这里-1也为true;
  if(str.indexOf('sss')){
      console.log('没找到sss');
  }
  console.log(str.lastIndexOf('llo'));//2

includes(find,start)

  • 该方法用于判断字符串是否包含指定的子字符串,如果是返回true,否则返回false
  • start设置从那个位置开始查找,默认为0
  • 方法区分大小写。
  var str = 'hello world!';
  console.log(str.includes('llo')); //true
  console.log(str.includes('llo', 5)); //false

str.match(find)

  • 查找字符串中指定的字符,如果找到就以数组形式返回这个字符,如果没找到返回null
  var str = 'hello world!';
  console.log(str.match('llo'));//["llo", index: 2, input: "hello world!"],以数组形式返回,内含查找的指定字符串
  console.log(str.match('llo')[0]);//llo
  console.log(str.match('llo')[0][2]);//o
  console.log(str.match('sss'));//null

str.search(find)

  • 检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
  • 返回匹配的String对象起始位置。
  • 没有找到任何匹配的子串,则返回-1
  var str = 'hello world!';
  console.log(str.search('llo')); //2
  console.log(str.search('sss')); //-1

  console.log(str.search(/wor/i)); //6

str.charAt(index)

  • 根据下标找对应字符。如果参数index不在0string.length之间,该方法将返回一个空字符串。
  var str = 'hello world!';
  console.log(str.charAt(0));//h
  console.log(str.charAt(6));//w
  console.log(str.charAt(-2));// ,如果是负值或者超出字符串本身长度,返回一个空字符串
  console.log(str.charAt(100));// ,如果是负值或者超出字符串本身长度,返回一个空字符串

str.charCodeAt(index)

  • 返回指定位置的字符的Unicode编码;返回值是0-65535之间的整数;如果index是负值或者超出字符串本身长度,返回NaN
  var str = 'hello world!';
  console.log(str.charCodeAt(6));//119
  console.log(str.charCodeAt(-2));//NaN,如果是负值或者超出字符串本身长度,返回NaN
  console.log(str.charCodeAt(100));//NaN,如果是负值或者超出字符串本身长度,返回NaN

String.fromCharCode(numX,numX,numX)

  • 根据Unicode编码,返回一个字符串;可设置多个Unicode,用逗号隔开。注意它不能作为您已创建的 String对象的方法来使用,它是根据编码找字符串。
  var str = 'hello world!';
  console.log(String.fromCharCode(65,66,67));//ABC
  //注意它不能作为您已创建的 String 对象的方法来使用
  var test = '';
  //console.log(test.fromCharCode(72));//报错 test.fromCharCode is not a function ,故注释掉
  console.log(String.fromCharCode(72));//H

str.startsWith(find,start) 和 str.endsWith(find,start)

  • 检测字符串是否以指定的子字符串开始。
  • 如果是以指定的子字符串开头返回true,否则false
  • startsWith()方法对大小写敏感。
  • start可选,查找的开始位置,默认为0
  var str = 'hello world!';
  console.log(str.startsWith('llo')); //false
  console.log(str.startsWith('he')); //true

  console.log(str.endsWith('he')); //false
  console.log(str.endsWith('d!')); //true
原文地址:https://www.cnblogs.com/sspeng/p/11491390.html