13_字符串的方法.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 sln = txt.length;
  14. // console.log(sln)
  15. //indexOf() 方法返回字符串中指定文本首次出现的索引(位置)
  16. // var str = "The full name of China is the People's Republic of China.";
  17. // var pos = str.indexOf("name");
  18. // console.log(pos)
  19. //lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引
  20. // var str = "The full name of China is the People's Republic of China.";
  21. // var pos = str.lastIndexOf("China");
  22. // console.log(pos)
  23. // 如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1
  24. // var str = "The full name of China is the People's Republic of China.";
  25. // var pos = str.lastIndexOf("baidu");
  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. // lastIndexOf() 方法向后进行检索(从尾到头),这意味着:假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点。
  31. // var str = "The full name of China is the People's Republic of China.";
  32. // var pos = str.lastIndexOf("China", 50);
  33. // console.log(pos)
  34. /* 这两种方法是不相等的。区别在于:
  35. search() 方法无法设置第二个开始位置参数。
  36. indexOf() 方法无法设置更强大的搜索值(正则表达式) */
  37. // var str = "The full name of China is the People's Republic of China.";
  38. // var pos = str.search("of");
  39. // console.log(pos)
  40. //slice() 提取字符串的某个部分并在新字符串中返回被提取的部分
  41. // var str = "Apple, Banana, Mango";
  42. // var res = str.slice(7, 13);
  43. // console.log(res)
  44. //如果某个参数为负,则从字符串的结尾开始计数
  45. // var str = "Apple, Banana, Mango";
  46. // var res = str.slice(-13, -7);
  47. // console.log(res)
  48. //substring() 类似于 slice()。不同之处在于 substring() 无法接受负的索引。
  49. // var str = "Apple, Banana, Mango";
  50. // var res = str.substring(7, 13);
  51. // console.log(res)
  52. // substr() 类似于 slice()。不同之处在于第二个参数规定被提取部分的长度。
  53. // var str = "Apple, Banana, Mango";
  54. // var res = str.substr(7, 6);
  55. // console.log(res)
  56. //replace() 方法用另一个值替换在字符串中指定的值
  57. //replace() 方法不会改变调用它的字符串。它返回的是新字符串
  58. // str = "Please visit Microsoft!";
  59. // var n = str.replace("Microsoft", "W3School");
  60. // console.log(n)
  61. // 默认地,replace() 只替换首个匹配
  62. //replace() 对大小写敏感。因此不对匹配 MICROSOFT
  63. // str = "Please visit Microsoft and Microsoft!";
  64. // var n = str.replace("Microsoft", "W3School");
  65. // console.log(n)
  66. //通过 toUpperCase() 把字符串转换为大写:
  67. // var text1 = "Hello World!"; // 字符串
  68. // var text2 = text1.toUpperCase(); // text2 是被转换为大写的 text1
  69. // console.log(text2)
  70. //通过 toLowerCase() 把字符串转换为小写:
  71. // var text1 = "Hello World!"; // 字符串
  72. // var text2 = text1.toLowerCase(); // text2 是被转换为小写的 text1
  73. // console.log(text2)
  74. //concat() 连接两个或多个字符串:
  75. // var text1 = "Hello";
  76. // var text2 = "World";
  77. // text3 = text1.concat(" ", text2);
  78. // console.log(text3)
  79. //trim() 方法删除字符串两端的空白符
  80. // var str = " Hello World! ";
  81. // alert(str.trim());
  82. // charAt() 方法返回字符串中指定下标(位置)的字符串
  83. // var str = "HELLO WORLD";
  84. // console.log(str.charAt(0))
  85. </script>
  86. </body>
  87. </html>