個人日記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() 則用字符位置和長度來指定子串。

 

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