JavaScript学习笔记:字符串处理

测试了以下功能:

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

代码如下:



  JavaScript学习




我使用的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

转载于:https://my.oschina.net/Tsybius2014/blog/1626470

你可能感兴趣的:(JavaScript学习笔记:字符串处理)