zsydgithub hai 1 ano
pai
achega
09d39769f5
Modificáronse 1 ficheiros con 132 adicións e 0 borrados
  1. 132 0
      js/15_字符串的方法.html

+ 132 - 0
js/15_字符串的方法.html

@@ -0,0 +1,132 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+
+<body>
+  <script>
+    /* length 属性返回字符串的长度: */
+    // var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    // var nn = txt.length
+    // console.log(nn)
+
+    /* indexOf() 方法返回字符串中指定文本首次出现的索引(位置): */
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.indexOf('China')
+    // console.log(pos)
+
+    /* lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引: */
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.lastIndexOf('one')
+    // console.log(pos)
+    /* 如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1 */
+
+    /* 两种方法都接受作为检索起始位置的第二个参数。 */
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.indexOf('China',20)
+    // console.log(pos)
+
+    /* lastIndexOf() 方法向后进行检索(从尾到头),这意味着:
+    假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点。 */
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.lastIndexOf('China',20)
+    // console.log(pos)
+
+
+    /* search() 方法搜索特定值的字符串,并返回匹配的位置: */
+    // var str = "The full name of name China is the People's Republic of China.";
+    // var pos = str.search("name");
+    // console.log(pos)
+    /* search() 方法无法设置第二个开始位置参数。
+      indexOf() 方法无法设置更强大的搜索值(正则表达式)。 */
+
+
+    /* slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。 */
+    // var str = "Apple, Banana, Mango";
+    // var res = str.slice(7, 13);
+    // var res = str.slice(-13, -7);
+    // console.log(res)
+
+    /* 如果省略第二个参数,则该方法将裁剪字符串的剩余部分: */
+    // var res = str.slice(7);
+    // console.log(res)
+
+    // var res = str.slice(-13);
+    // console.log(res)
+
+    /* substring() 类似于 slice()。
+      不同之处在于 substring() 无法接受负的索引。 */
+    // var str = "Apple, Banana, Mango";
+    // var res = str.substring(7, 13);
+    // console.log(res)
+
+    /* 如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分 */
+    // var res = str.substring(7);
+    // console.log(res)
+
+    /* substr() 类似于 slice()。
+      不同之处在于第二个参数规定被提取部分的长度。 */
+
+    // var str = "Apple, Banana, Mango";
+    // var res = str.substr(7, 6);
+    // console.log(res)
+    /* 第二个参数不能为负,因为它定义的是长度。 */
+
+
+    /* replace() 方法用另一个值替换在字符串中指定的值: */
+
+    // str = "Please visit Microsoft!";
+    // var n = str.replace("Microsoft", "W3School");
+    // console.log(n)
+    // console.log(str)
+
+    /* replace() 方法不会改变调用它的字符串。它返回的是新字符串。
+      默认地,replace() 只替换首个匹配: */
+
+    // str = "Please visit Microsoft and Microsoft!";
+    // var n = str.replace("Microsoft", "W3School");
+    // console.log(n)
+
+    /* 默认地,replace() 对大小写敏感。因此不对匹配 MICROSOFT */
+    // str = "Please visit Microsoft!";
+    // var n = str.replace("MICROSOFT", "W3School");
+    // console.log(n)
+
+    /* 通过 toUpperCase() 把字符串转换为大写: */
+    // var text1 = "Hello World!";       // 字符串
+    // var text2 = text1.toUpperCase();  // text2 是被转换为大写的 text1
+    // console.log(text1)
+    // console.log(text2)
+
+    /* 通过 toLowerCase() 把字符串转换为小写:*/
+    // var text1 = "Hello World!";       // 字符串
+    // var text2 = text1.toLowerCase();  // text2 是被转换为小写的 text1
+    // console.log(text1)
+    // console.log(text2)
+
+    /* concat() 连接两个或多个字符串: */
+    // var text1 = "Hello";
+    // var text2 = "World";
+    // text3 = text1.concat(" ", text2,1,4);
+    // console.log(text3)
+
+    /* concat() 方法可用于代替加运算符。下面两行是等效的: */
+    // var text = "Hello" + " " + "World!";
+    // var text = "Hello".concat(" ", "World!");
+
+    /* trim() 方法删除字符串两端的空白符: */
+    // var str = "       Hello World!        ";
+    // alert(str.trim());
+
+    /* charAt() 方法返回字符串中指定下标(位置)的字符串: */
+    var str = "HELLO WORLD";
+    console.log(str.charAt(0))
+  </script>
+</body>
+
+</html>