15_字符串的方法.html 4.7 KB

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