测试了以下功能:
1 判断字符串是否为空
2 判断字符串是否为空串
3 判断字符串长度 length
4 去掉左侧空格 trimLeft trimRight trim
5 获取字符串子串第一次出现的位置 indexOf lastIndexOf
6 截取字符串子串 substring substr
7 获取第n个字符 charAt
8 小写转大写 toUpperCase 大写转小写 toLowerCase
9 字符串分割 split
10 截取字符串 slice
11 替换字符串 replace
12 正则匹配返回索引 search
13 正则匹配 match
代码如下:
<html>
<head>
<title>JavaScript学习</title>
</head>
<body>
</body>
<script>
//1 判断字符串是否为空
var a = null;
if (a == null) {
console.log("a为空"); // a为空
} else {
console.log("a不为空");
}
console.log("---------------------");
//2 判断字符串是否为空串
var b = "";
if (b == "") {
console.log("b为空串"); // b为空串
} else {
console.log("b不为空串");
}
console.log("---------------------");
//3 判断字符串长度 length
var c = "1234567";
console.log("c的长度:" + c.length); // 7
console.log("---------------------");
//4 去掉左侧空格 trimLeft trimRight trim
var d = " 12345 ";
console.log("d去掉左侧空格[" + d.trimLeft() + "]"); // [12345 ]
console.log("d去掉右侧空格[" + d.trimRight() + "]"); // [ 12345]
console.log("d去掉两侧空格[" + d.trim() + "]"); // [12345]
console.log("---------------------");
//5 获取字符串子串第一次出现的位置 indexOf lastIndexOf
var e = "12345678901234567890";
var eIndex1 = e.indexOf("34567");
console.log("子串34567第一次出现在位置" + eIndex1); // 2
var eIndex2 = e.indexOf("34567", 10);
console.log("子串34567在第10个字符后第一次出现在位置" + eIndex2); // 12
var eIndex3 = e.indexOf("x");
console.log("子串x第一次出现在位置" + eIndex3); // -1
var eIndex4 = e.lastIndexOf("34567");
console.log("子串34567最后一次出现在位置" + eIndex4); // 12
var eIndex5 = e.lastIndexOf("x");
console.log("子串x最后一次出现在位置" + eIndex5); // -1
console.log("---------------------");
//6 截取字符串子串 substring substr
var f = "12345678901234567890";
var fSub1 = f.substring(5); //截取第5个字符以后的内容 -- 678901234567890
console.log("字符截取结果:" + fSub1);
var fSub2 = f.substr(5); //截取第5个字符以后的内容 -- 678901234567890
console.log("字符截取结果:" + fSub2);
var fSub3 = f.substring(5, 10); //截取第5个字符后到第10个字符的内容 -- 67890
console.log("字符截取结果:" + fSub3);
var fSub4 = f.substr(5, 10); //截取第5个字符后的10个字符 -- 6789012345
console.log("字符截取结果:" + fSub4);
var fSub5 = f.substring(25); //截取第25个字符以后的内容 -- 空串
console.log("字符截取结果:" + fSub5);
var fSub6 = f.substr(25); //截取第25个字符以后的内容 -- 空串
console.log("字符截取结果:" + fSub6);
console.log("---------------------");
//7 获取第n个字符 charAt
var g = "abcdefghijklmnopqrstuvwxyz";
var gCharAt0 = g.charAt(0);
console.log("第0个字符:" + gCharAt0); // a
var gCharAt5 = g.charAt(5);
console.log("第0个字符:" + gCharAt5); // f
var gCharAt30 = g.charAt(30);
console.log("第0个字符:" + gCharAt30); // 空串
console.log("---------------------");
//8 小写转大写 toUpperCase 大写转小写 toLowerCase
var h = "abcdefghijklmnopqrstuvwxyz";
var hToUpper = h.toUpperCase();
console.log("转大写:" + hToUpper); // ABCDEFGHIJKLMNOPQRSTUVWXYZ
var i = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var iToLower = i.toLowerCase();
console.log("转小写:" + iToLower); // abcdefghijklmnopqrstuvwxyz
console.log("---------------------");
//9 字符串分割 split
var j = "abcdefg|hijklmn|opqrst|uvwxyz";
var j1 = j.split("|");
console.log(j1); // 使用字符串分割 -- ["abcdefg", "hijklmn", "opqrst", "uvwxyz"]
var j2 = j.split("2018");
console.log(j2); //使用字符串分割 -- ["abcdefg|hijklmn|opqrst|uvwxyz"]
var j3 = j.split(/[a-zA-Z]\|[a-zA-Z]/);
console.log(j3); // 使用正则表达式分割 -- ["abcdef", "ijklm", "pqrs", "vwxyz"]
var j4 = j.split();
console.log(j4); //无参数情况 -- ["abcdefg|hijklmn|opqrst|uvwxyz"]
console.log("---------------------");
//10 截取字符串 slice
var k = "abcdefghijklmnopqrstuvwxyz";
var kSlice1 = k.slice(0, 1);
console.log(kSlice1); // a
var kSlice2 = k.slice(0, 5);
console.log(kSlice2); // abcde
var kSlice2 = k.slice(0, 30);
console.log(kSlice2); // abcdefghijklmnopqrstuvwxyz
var kSlice2 = k.slice(5, 10);
console.log(kSlice2); // fghij
var kSlice2 = k.slice(5, 0);
console.log(kSlice2); // 空串
var kSlice2 = k.slice(5, -1);
console.log(kSlice2); // fghijklmnopqrstuvwxy
var kSlice2 = k.slice(5, -10);
console.log(kSlice2); // fghijklmnop
var kSlice2 = k.slice(5, -25);
console.log(kSlice2); // 空串
var kSlice2 = k.slice(5, -30);
console.log(kSlice2); // 空串
console.log("---------------------");
//11 替换字符串 replace
var l = "abcdefg hijklmn abcdefg hijklmn";
var lReplace1 = l.replace("abcdefg", "x");
console.log(lReplace1); // 替换第一个abcdefg为x -- x hijklmn abcdefg hijklmn
var lReplace2 = l.replace(/abcdefg/, "x");
console.log(lReplace2); // 替换第一个abcdefg为x -- x hijklmn abcdefg hijklmn
var lReplace3 = l.replace(/abcdefg/g, "x");
console.log(lReplace3); // 替换所有的abcdefg为x -- x hijklmn x hijklmn
var lReplace4 = l.replace(/abcdefg/g, function(word){ return word.toUpperCase(); });
console.log(lReplace4); // 替换所有的abcdefg为大写 -- ABCDEFG hijklmn ABCDEFG hijklmn
var m = "aBcDeFg hijklmn aBcDeFg hijklmn";
var mReplace1 = m.replace(/AbCdEfG/i, "x");
console.log(mReplace1); // 替换所有的abcdefg为x(大小写不敏感) -- x hijklmn x hijklmn
console.log("---------------------");
//12 正则匹配返回索引 search
var n = "abcdefghijklmnopqrstuvwxyz";
var nIndex1 = n.search(/hijklmn/); // 7
console.log(nIndex1);
var nIndex2 = n.search(/HIJKLMN/); // -1
console.log(nIndex2);
var nIndex3 = n.search(/HIJKLMN/i); // 7
console.log(nIndex3);
var nIndex4 = n.search("hijklmn"); // 7
console.log(nIndex4);
console.log("---------------------");
//13 正则匹配 match
var o = "abcdefghijklmnABCDEFGHIJKLMN";
var oMatch1 = o.match(/abcdefg/);
console.log(oMatch1); // 匹配abcdefg -- ["abcdefg"]
var oMatch2 = o.match(/abcdefg/gi);
console.log(oMatch2); // 匹配abcdefg(大小写不敏感) -- ["abcdefg", "ABCDEFG"]
var oMatch3 = o.match(/x/);
console.log(oMatch3); // 匹配x -- null
</script>
</html>
我使用的Chrome版本为 Version 63.0.3239.132 (Official Build) (64-bit)
运行结果如下:
js-study.html:12 a为空
js-study.html:17 ---------------------
js-study.html:22 b为空串
js-study.html:27 ---------------------
js-study.html:31 c的长度:7
js-study.html:33 ---------------------
js-study.html:37 d去掉左侧空格[12345 ]
js-study.html:38 d去掉右侧空格[ 12345]
js-study.html:39 d去掉两侧空格[12345]
js-study.html:41 ---------------------
js-study.html:46 子串34567第一次出现在位置2
js-study.html:48 子串34567在第10个字符后第一次出现在位置12
js-study.html:50 子串x第一次出现在位置-1
js-study.html:52 子串34567最后一次出现在位置12
js-study.html:54 子串x最后一次出现在位置-1
js-study.html:56 ---------------------
js-study.html:61 字符截取结果:678901234567890
js-study.html:63 字符截取结果:678901234567890
js-study.html:65 字符截取结果:67890
js-study.html:67 字符截取结果:6789012345
js-study.html:69 字符截取结果:
js-study.html:71 字符截取结果:
js-study.html:73 ---------------------
js-study.html:78 第0个字符:a
js-study.html:80 第0个字符:f
js-study.html:82 第0个字符:
js-study.html:84 ---------------------
js-study.html:89 转大写:ABCDEFGHIJKLMNOPQRSTUVWXYZ
js-study.html:92 转小写:abcdefghijklmnopqrstuvwxyz
js-study.html:94 ---------------------
js-study.html:99 (4) ["abcdefg", "hijklmn", "opqrst", "uvwxyz"]
js-study.html:101 ["abcdefg|hijklmn|opqrst|uvwxyz"]
js-study.html:103 (4) ["abcdef", "ijklm", "pqrs", "vwxyz"]
js-study.html:105 ["abcdefg|hijklmn|opqrst|uvwxyz"]
js-study.html:107 ---------------------
js-study.html:112 a
js-study.html:114 abcde
js-study.html:116 abcdefghijklmnopqrstuvwxyz
js-study.html:118 fghij
js-study.html:120
js-study.html:122 fghijklmnopqrstuvwxy
js-study.html:124 fghijklmnop
js-study.html:126
js-study.html:128
js-study.html:130 ---------------------
js-study.html:135 x hijklmn abcdefg hijklmn
js-study.html:137 x hijklmn abcdefg hijklmn
js-study.html:139 x hijklmn x hijklmn
js-study.html:141 ABCDEFG hijklmn ABCDEFG hijklmn
js-study.html:144 x hijklmn aBcDeFg hijklmn
js-study.html:146 ---------------------
js-study.html:151 7
js-study.html:153 -1
js-study.html:155 7
js-study.html:157 7
js-study.html:159 ---------------------
js-study.html:164 ["abcdefg", index: 0, input: "abcdefghijklmnABCDEFGHIJKLMN"]
js-study.html:166 (2) ["abcdefg", "ABCDEFG"]
js-study.html:168 null
END