个人日记020--JS字符串操作方法

借鉴了W3School的JavaScript String 对象

txt="Hello World!"
1.Big: 用大号字体显示字符串,用法:txt.big(),结果:Hello World!

2.Small: 使用小字号来显示字符串,用法:txt.small(),结果:Hello World!

3.Bold: 使用粗体显示字符串,用法:txt.bold(),结果:Hello World!

4.Italic: 使用斜体显示字符串,用法:txt.italics(),结果:Hello World!

5.Fixed: 以打字机文本显示字符串,用法:txt.fixed(),结果:Hello World!

6.Strike: 使用删除线来显示字符串,用法:txt.strike(),结果:Hello World!

7.Fontcolor: 使用指定的颜色来显示字符串,用法:txt.fontcolor('Red'),结果:Hello World!

8.Lowercase: 把字符串转换为小写,用法:txt.toLowerCase(),结果:hello world!

9.Uppercase: 把字符串转换为大写,用法:txt.toUpperCase(),结果:HELLO WORLD!

10.Subscript: 把字符串显示为下标,用法:txt.sub(),结果:Hello World!

11.Superscript: 把字符串显示为上标,用法:txt.sup(),结果:Hello World!

12.Link: 将字符串显示为链接,用法:txt.link('http://www.w3school.com.cn'),结果:Hello World!

13.charAt: 返回在指定位置的字符,用法:txt.charAt(6),结果:W

14.fromCharCode: 返回在指定的位置的字符的 Unicode 编码,用法:txt.fromCharCode(65,66,67),结果:ABC

15.concat: 连接字符串,用法:txt.concat('concat',13579),结果:Hello World!concat13579

16.indexOf: 检索字符串,用法:txt.indexOf('llo'),结果:2

17.lastIndexOf: 从后向前搜索字符串,用法:txt.lastIndexOf('Wor'),结果:6

18.match: 找到一个或多个正则表达式的匹配,用法:txt.match('orl'),结果:orl

19.replace: 替换与正则表达式匹配的子串,用法:txt.replace('World','W3School'),结果:Hello W3School!

20.search: 检索与正则表达式相匹配的值,用法:txt.search('orl'),结果:7

21.slice: 提取字符串的片断,并在新的字符串中返回被提取的部分,用法:txt.slice(5),结果: World!

22.split: 把字符串分割为字符串数组,用法:txt.split(''),结果:H,e,l,l,o, ,W,o,r,l,d,!

23.substr: 从起始索引号提取字符串中指定数目的字符,用法:txt.substr(-1),结果:!

24.substring: 提取字符串中两个指定的索引号之间的字符,用法:txt.substring(4,1),结果:ell

indexOf和lastIndexOf如果要检索的字符串值没有出现,则返回 -1
String 对象的方法 slice()、substring() 和 substr() (不建议使用)都可返回字符串的指定部分。slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。slice() 与 substr() 有所不同,因为它用两个字符的位置来指定子串,而 substr() 则用字符位置和长度来指定子串。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章