JavaScript String(字符串)對象的簡單實例(推薦)
返回字符串的長度:
<html> <body> <script type="text/javascript"> var txt="Hello World!" document.write(txt.length) </script> </body> </html>
為字符串添加樣式:
<html> <body> <script type="text/javascript"> var txt="Hello World!" document.write("<p>Big: " + txt.big() + "</p>") document.write("<p>Small: " + txt.small() + "</p>") document.write("<p>Bold: " + txt.bold() + "</p>") document.write("<p>Italic: " + txt.italics() + "</p>") document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>") document.write("<p>Fixed: " + txt.fixed() + "</p>") document.write("<p>Strike: " + txt.strike() + "</p>") document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>") document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>") document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>") document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>") document.write("<p>Subscript: " + txt.sub() + "</p>") document.write("<p>Superscript: " + txt.sup() + "</p>") document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>") </script> </body> </html>
返回字符串中指定文本首次出現(xiàn)的位置 - indexOf()方法:
<html> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) </script> </body> </html>
效果如下:
查找字符串中特定的字符,若找到,則返回該字符 - match() 方法:
<html> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.match("world") + "<br />") document.write(str.match("World") + "<br />") document.write(str.match("worlld") + "<br />") document.write(str.match("world!")) </script> </body> </html>
效果如下:
替換字符串中的字符 - replace():
<html> <body> <script type="text/javascript"> var str="Visit Microsoft!" document.write(str.replace(/Microsoft/,"W3School")) </script> </body> </html>
效果如下:
以上這篇JavaScript String(字符串)對象的簡單實例(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript獲取時區(qū)實現(xiàn)過程解析
這篇文章主要介紹了JavaScript獲取時區(qū)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09JS hasOwnProperty()方法檢測一個屬性是否是對象的自有屬性的方法
這篇文章主要介紹了JS hasOwnProperty()方法檢測一個屬性是否是對象的自有屬性的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01在Javascript里訪問SharePoint列表數(shù)據(jù)的實現(xiàn)方法
在進(jìn)行SharePoint訂制的時候經(jīng)常會遇到開發(fā)手段受限制的問題,比如通常公司都會限制服務(wù)器的訪問以及部署,很多開發(fā)都只能夠在客戶端來進(jìn)行2011-05-05