zsydgithub 2 năm trước cách đây
mục cha
commit
830ac7cd02
1 tập tin đã thay đổi với 101 bổ sung0 xóa
  1. 101 0
      4_JS基础/17_字符串方法.html

+ 101 - 0
4_JS基础/17_字符串方法.html

@@ -0,0 +1,101 @@
+<!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 a = txt.length; 
+    // console.log(a)
+
+    // indexOf() 方法返回字符串中指定文本首次出现的索引(位置)
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.indexOf("China");
+    // console.log(pos)
+
+    //lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引
+    //如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.lastIndexOf("cake");
+    // console.log(pos)
+
+
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.search("China");
+    // console.log(pos)
+
+    // var str = "The full name of China is the People's Republic of China.";
+    // var pos = str.indexOf("China", 18);
+    // console.log(pos)
+
+    //slice() 提取字符串的某个部分并在新字符串中返回被提取的部分
+    // var str = "Apple, Banana, Mango";
+    // var res = str.slice(7, 13);
+    // console.log(res)
+
+    //如果某个参数为负,则从字符串的结尾开始计数
+    // var str = "Apple, Banana, Mango";
+    // // var res = str.slice(-13, -7);
+    // var res = str.slice(7);
+    // console.log(res)
+
+
+    //substring() 类似于 slice()。
+    //不同之处在于 substring() 无法接受负的索引
+    // var str = "Apple, Banana, Mango";
+    // var res = str.substring(7, 13);
+    // console.log(res)
+
+    //不同之处在于第二个参数规定被提取部分的长度
+    //如果省略第二个参数,则该 substr() 将裁剪字符串的剩余部分
+    // var str = "Apple, Banana, Mango";
+    // var res = str.substr(7, 6);
+    // console.log(res)
+
+
+    //replace() 方法用另一个值替换在字符串中指定的值
+    //默认地,replace() 只替换首个匹配
+    // str = "Please visit Microsoft!";
+    // var n = str.replace("Microsoft", "W3School");
+    // console.log(n)
+
+    //通过 toUpperCase() 把字符串转换为大写
+    // var text1 = "Hello World!";
+    // var text2 = text1.toUpperCase();
+    // console.log(text2)
+
+    //通过 toLowerCase() 把字符串转换为小写
+    // var text1 = "Hello World!";       
+    // var text2 = text1.toLowerCase()
+    // console.log(text2)
+
+    //concat() 连接两个或多个字符串
+    var text1 = "Hello";
+    var text2 = "World";
+    text3 = text1.concat(" ", text2);
+    console.log(text3)
+
+    //trim() 方法删除字符串两端的空白符
+    //警告:Internet Explorer 8 或更低版本不支持 trim() 方法。
+    // var str = "       Hello World!        ";
+    // alert(str.trim());
+
+    //charAt() 方法返回字符串中指定下标(位置)的字符串
+    // var str = "HELLO WORLD";
+    // var a= str.charAt(0);   
+    // console.log(a)
+
+    //可以通过 split() 将字符串转换为数组
+    var txt = "a,b,c,d,e";   // 字符串
+    console.log(txt.split(","))    
+  </script>
+</body>
+
+</html>