The following functions were tested:
1 Check if the string is empty
2 Determine whether the string is an empty string
3 Determine the length of the string
4 Remove the left space trimLeft trimRight trim
5 Get the position of the first occurrence of a string substring indexOf lastIndexOf
6 Intercept string substring substring substr
7 Get the nth character charAt
8 lowercase to uppercase toUpperCase uppercase to lowercase toLowerCase
9 String split split
10 Intercept string slice
11 replace string replace
12 Regular match returns index search
13 Regular match match
code show as below:
<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>
The version of Chrome I am using is Version 63.0.3239.132 (Official Build) (64-bit)
The results are as follows:
js-study.html:12 a is empty
js-study.html:17 ---------------------
js-study.html:22 b is empty string
js -study.html:27 ---------------------
js-study.html:31 Length of c:7
js-study.html:33 ---- -----------------
js-study.html:37 d remove the left space [12345]
js-study.html:38 d remove the right space [12345]
js-study .html:39 d remove the spaces on both sides [12345]
js-study.html:41 ---------------------
js-study.html:46 Substring 34567 First occurrence
of substring 34567 at position 2 js-study.html:48 First occurrence of substring x after 10th character at position 12
js-study.html:50 First occurrence of substring x at position -1
js- study.html:52 substring 34567 was last seen at position 12
js-study.html:54 substring x was last seen at position-1
js-study.html:56 ------------ ---------
js-study.html:61 Character interception result: 678901234567890
js-study.html:63 Character interception result: 678901234567890
js-study.html: 65 character interception result: 67890
js-study.html:67 character interception result: 6789012345
js-study.html:69 character interception result:
js-study.html:71 Character interception result:
js-study. html:73 ---------------------
js-study.html:78 0th character: a
js-study.html:80 0th character: f
js-study.html:82 The 0th character:
js-study.html:84 ---------------------
js-study.html:89 Capitalize: ABCDEFGHIJKLMNOPQRSTUVWXYZ
js-study.html:92 lowercase: 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