17_字符串方法.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. //length 属性返回字符串的长度
  12. // var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  13. // var a = txt.length;
  14. // console.log(a)
  15. // indexOf() 方法返回字符串中指定文本首次出现的索引(位置)
  16. // var str = "The full name of China is the People's Republic of China.";
  17. // var pos = str.indexOf("China");
  18. // console.log(pos)
  19. //lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引
  20. //如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
  21. // var str = "The full name of China is the People's Republic of China.";
  22. // var pos = str.lastIndexOf("cake");
  23. // console.log(pos)
  24. // var str = "The full name of China is the People's Republic of China.";
  25. // var pos = str.search("China");
  26. // console.log(pos)
  27. // var str = "The full name of China is the People's Republic of China.";
  28. // var pos = str.indexOf("China", 18);
  29. // console.log(pos)
  30. //slice() 提取字符串的某个部分并在新字符串中返回被提取的部分
  31. // var str = "Apple, Banana, Mango";
  32. // var res = str.slice(7, 13);
  33. // console.log(res)
  34. //如果某个参数为负,则从字符串的结尾开始计数
  35. // var str = "Apple, Banana, Mango";
  36. // // var res = str.slice(-13, -7);
  37. // var res = str.slice(7);
  38. // console.log(res)
  39. //substring() 类似于 slice()。
  40. //不同之处在于 substring() 无法接受负的索引
  41. // var str = "Apple, Banana, Mango";
  42. // var res = str.substring(7, 13);
  43. // console.log(res)
  44. //不同之处在于第二个参数规定被提取部分的长度
  45. //如果省略第二个参数,则该 substr() 将裁剪字符串的剩余部分
  46. // var str = "Apple, Banana, Mango";
  47. // var res = str.substr(7, 6);
  48. // console.log(res)
  49. //replace() 方法用另一个值替换在字符串中指定的值
  50. //默认地,replace() 只替换首个匹配
  51. // str = "Please visit Microsoft!";
  52. // var n = str.replace("Microsoft", "W3School");
  53. // console.log(n)
  54. //通过 toUpperCase() 把字符串转换为大写
  55. // var text1 = "Hello World!";
  56. // var text2 = text1.toUpperCase();
  57. // console.log(text2)
  58. //通过 toLowerCase() 把字符串转换为小写
  59. // var text1 = "Hello World!";
  60. // var text2 = text1.toLowerCase()
  61. // console.log(text2)
  62. //concat() 连接两个或多个字符串
  63. var text1 = "Hello";
  64. var text2 = "World";
  65. text3 = text1.concat(" ", text2);
  66. console.log(text3)
  67. //trim() 方法删除字符串两端的空白符
  68. //警告:Internet Explorer 8 或更低版本不支持 trim() 方法。
  69. // var str = " Hello World! ";
  70. // alert(str.trim());
  71. //charAt() 方法返回字符串中指定下标(位置)的字符串
  72. // var str = "HELLO WORLD";
  73. // var a= str.charAt(0);
  74. // console.log(a)
  75. //可以通过 split() 将字符串转换为数组
  76. var txt = "a,b,c,d,e"; // 字符串
  77. console.log(txt.split(","))
  78. </script>
  79. </body>
  80. </html>