1、字符串Unicode表示法

  1. console.log("\u0061");

2、字符串遍历器接口,for of

  1. <script>
  2. let str = 'abcdefg';
  3. for(let i=0;i<str.length;i++){
  4. console.log(str[i]);
  5. }
  6. for (let s of str) {
  7. console.log(s);
  8. }
  9. </script>

3、模板字符串方法

  1. <body>
  2. <div id="contianer"></div>
  3. <div id="contianer2"></div>
  4. <script>
  5. //es5字符串拼接方法
  6. var a;
  7. var href = "https://baidu.com";
  8. var text = "百度";
  9. a = "<a href='"+href+"'>"+text+"</a>";
  10. console.log(a);
  11. var contianer = document.getElementById("contianer");
  12. contianer.innerHTML = a;
  13. //es6字符串模板方法
  14. let aa = `<a href='${href}'>${text}</a>`
  15. var contianer2 = document.getElementById("contianer2");
  16. contianer2.innerHTML = aa;
  17. </script>
  18. </body>

4、字符包含、以某字符开头、结尾方法

  1. <script>
  2. let str = 'abcdefg';
  3. //只有一个参数时
  4. console.log(str.includes('abc')); //true
  5. console.log(str.startsWith('abc')); //true
  6. console.log(str.endsWith('fg')); //true
  7. //添加第二个参数时代表是从那个下标位置开始搜索
  8. console.log(str.includes('abc',2)); //false
  9. console.log(str.startsWith('def',3)); //true
  10. console.log(str.endsWith('abcde',5)); //true
  11. </script>

5、字符重复、字符补全

  1. <script>
  2. let str = 'a';
  3. console.log(str.repeat(2));
  4. console.log(str.padStart(10,"12345"));
  5. console.log(str.padEnd(10,"123"));
  6. </script>

6、空格去除

  1. <script>
  2. let str = ' A ';
  3. console.log(str.trim());
  4. console.log(str.trimStart());
  5. console.log(str.trimEnd());
  6. </script>

7、指定获取某个下标的字符

  1. <script>
  2. let str = 'abcdefg';
  3. console.log(str.at(1));
  4. console.log(str.charAt(1));
  5. </script>