length // 字符串的长度

  1. // 1.length // 字符串的长度
  2. var str1 = 'asdfasdfasdf';
  3. console.log('str1', str1.length);

trim() // 去掉字符串前后的空格

  1. // 2.trim() // 去掉字符串前后的空格
  2. var str2 = ' aaaaaaaaa ';
  3. var newStr2 = str2.trim();
  4. console.log('str2', str2);
  5. console.log('newStr2', newStr2);

toLowerCase() // 转小写字母

  1. // 3.toLowerCase() // 转小写字母
  2. var str3 = 'asdfASDFasdfs';
  3. var newStr3 = str3.toLowerCase();
  4. console.log('newStr3', newStr3);

toUpperCase() // 转大写字母

  1. var str4 = 'asdfASDFasdfs';
  2. var newStr4 = str4.toUpperCase();
  3. console.log('newStr4', newStr4);

charAt() // 返回字符的位置, 若没有,返回-1

  1. // 5.charAt() // 返回某个下表对应的字符
  2. var str5 = 'abcde';
  3. var chat5 = str5.charAt(1);
  4. console.log('chat5', chat5);

indexOf() // 返回检测字符串的位置

  1. // 6.indexOf() // 返回字符(字符串)在原字符串中的的位置
  2. var str6 = 'abcdef';
  3. var index6 = str6.indexOf('b');
  4. console.log('index6=', index6);

lastIndexOf() // 同上,但从后面开始检查

  1. // 7.lastIndexOf() // 同上,但从后面开始检查
  2. var str7 = 'abcdebf';
  3. var index7 = str7.lastIndexOf('b');
  4. console.log('index7=', index7);

includes() // 检查字符串是否包含指定字符

  1. // 8.includes() // 检查字符串是否包含指定字符
  2. var str8 = '我是中国人';
  3. var boo8 = str8.includes('我');
  4. console.log('boo8', boo8); // true

split() // 字符串转数组,接收一个参数,作为转换的标志

  1. // 9.split() // 字符串转数组,接收一个参数,作为转换的标志
  2. var str9 = '刘享高危,林玉生';
  3. var arr9 = str9.split(',');
  4. console.log('arr9=', arr9);

substring() // 截取字符串,接收两个参数,开始的位置和结束的位置

  1. // 10.substring() // 接收两个参数,开始的位置和结束的位置(不含), 有了slice,就很少substring
  2. var str10 = 'abcdefg';
  3. var newStr10 = str10.substring(1, 4);
  4. console.log('newStr10=', newStr10) // bcd

slice() // 截取字符串, 用法类似substring, 但可以复数

  1. // 11.slice() 截取字符串(极常用), 用法类似substring, 但可以是负数
  2. var str11 = 'abcdefg';
  3. var newStr11 = str11.slice(1, 4);
  4. console.log('newStr11=', newStr11) // bcd
  5. var newStr11 = str11.slice(1, -1); // 参数可以是负数
  6. console.log('newStr11=', newStr11) // bcdef
  7. var newStr11 = str11.slice(-1); // 第二个参数不给,从参数的位置开始,一直截取到最后
  8. console.log('newStr11=', newStr11) // g
  9. var phone = 13811112222;
  10. var phoneStr = phone + '';
  11. var newStr11 = phoneStr.slice(-4); // 从-4的位置开始截取到末尾
  12. console.log('newStr11=', newStr11);

substr() // 截取字符串, 接收两个参数, 开始位置, 第二个参数是截取的字符个数

  1. // 12.substr() 截取字符串, 接收两个参数,第一个参数是开始位置, 第二个参数是截取的字符个数
  2. var str12 = 'abcdef';
  3. var newStr12 = str12.substr(1, 3);
  4. console.log('newStr12', newStr12);

match() // 检查有没有包含某个值, 返回符合一个数组,存放符合条件的字符

  1. // 13.match() // 检查有没有包含某个值, 返回符合一个数组,存放符合条件的字符
  2. var str13 = 'abcdeabcdabcd';
  3. var arr13 = str13.match('a');
  4. console.log('arr13', arr13);
  5. // 正则
  6. var arr13 = str13.match(/a/g);
  7. console.log('arr13', arr13);

replace() // 替换 (替换全部,使用replaceAll或者正则)

  1. // 14.replace() // 替换
  2. var str14 = '张三,李四,王五,张三,陈六,张三';
  3. var newStr14 = str14.replace('张三', '张无忌');
  4. console.log('newStr14=', newStr14);
  5. // 15.替换全部,使用replaceAll或者正则
  6. var newStr14 = str14.replaceAll('张三', '张无忌');
  7. console.log('newStr14=', newStr14);
  8. var newStr14 = str14.replace(/张三/g, '张无忌');
  9. console.log('newStr14=', newStr14);