|
|
@@ -0,0 +1,43 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <script>
|
|
|
+ // 字符型对应的js内部顶级对象 String
|
|
|
+ var str = "hello";
|
|
|
+ var str2 = "world";
|
|
|
+ // 获取制定位置的字符 字符串起始位置是0
|
|
|
+ console.log( str.charAt(0) );
|
|
|
+ // 拼接字符串
|
|
|
+ console.log(str.concat(str2));
|
|
|
+ // 返回某个指定的字符串值在字符串中首次出现的位置。如果没有包含这个字符返回-1
|
|
|
+ console.log(str.indexOf("w"));
|
|
|
+ // 查找字符串中是否包含指定的子字符串。
|
|
|
+ console.log(str.includes("hl"));
|
|
|
+ // 提取字符串的片断,并在新的字符串中返回被提取的部分。
|
|
|
+ // 第一个参数开始位置 第二参数结束位置(不包含结束位置)
|
|
|
+ // 如果第二个参数不写截取到最后
|
|
|
+ console.log(str.slice(1,4));
|
|
|
+ // 从起始索引号提取字符串中指定数目的字符。
|
|
|
+ // 第二个参数为截取个数
|
|
|
+ console.log(str.substr(1,4))
|
|
|
+ // trim 去除左右两边空格
|
|
|
+ var str3 = " 你好 ";
|
|
|
+ console.log(str3);
|
|
|
+ console.log(str3.trim());
|
|
|
+ // 把字符串分割为字符串数组。
|
|
|
+ var str4 = "2026-1-9";
|
|
|
+ console.log(str4.split("-"));
|
|
|
+ var str5 = "abcdefg";
|
|
|
+ console.log(str5.split(""));
|
|
|
+ // 获取字符串长度
|
|
|
+ console.log(str.length);
|
|
|
+
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|