21.字符串的方法.html 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. var str = 'Happy New Year';
  11. console.log(str,'原本的')
  12. //toUpperCase 将字符串中所有字符转成大写
  13. console.log(str.toUpperCase());
  14. //toLowerCase 将字符串中所有字符转成小写
  15. console.log(str.toLowerCase());
  16. var str2 = 'hi';
  17. // concat 将字符串连接在一起
  18. var str1 = str.concat(str2);
  19. console.log(str1);
  20. var str3 = ' hello , w orld ';
  21. console.log(str3);
  22. // 4.trim 取消字符串前后空格
  23. console.log(str3.trim())
  24. var str4 = 'This is a cup is';
  25. // indexOf 查找字符首次出现的下标索引位置
  26. // console.log(str4.indexOf("is"));
  27. // // lastIndexOf 查找字符最后出现的下标索引位置
  28. // console.log(str4.lastIndexOf("is"));
  29. // 截取 slice
  30. // 一个值:从哪个下标开始截取到最后,负数从-1开始
  31. // 两个值: 第一个值开始截取的下标索引 第二个结束的下标索引 但不包含该索引
  32. // console.log(str4.slice(-5,-2))
  33. // 截取 substr
  34. // 一个值:从哪个下标开始截取到最后,负数从-1开始
  35. // 两个值: 第一个值开始截取的下标索引 第二个代表截取的长度
  36. // 截取 substring
  37. // 一个值:从哪个下标开始截取到最后
  38. // 两个值: 第一个值开始截取的下标索引 第二个结束的下标索引 但不包含该索引
  39. // console.log(str4.substr(3))
  40. // console.log(str4.substring(5,9))
  41. var str5 = 'hi~hello~hi';
  42. // replace 替换
  43. // console.log(str5.replace("hi",'图图'));
  44. // console.log()
  45. // split 字符串转数组
  46. // console.log(str5.split("~"))
  47. // charAt 查找当前下标的字符
  48. console.log(str5.charAt(7))
  49. </script>
  50. </body>
  51. </html>