Javascript:字串對象(string)的基本操作,javascriptstring

來源:互聯網
上載者:User

Javascript:字串對象(string)的基本操作,javascriptstring

本文代碼整理自w3school:http://www.w3school.com.cn  

1.擷取字串的長度:

var s = "Hello world";document.write("length:"+s.length);
2.為字串添加各種樣式,如:

var txt = "Some words";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>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")
3.擷取字串中部分內容首次出現的位置:

var hw_text = "Hello world";document.write(hw_text.indexOf("Hello")+"<br/>");document.write(hw_text.indexOf("world")+"<br/>");document.write(hw_text.indexOf("abc")+"<br/>");
4.內容替換:

var str="Visit Microsoft!"document.write(str.replace(/Microsoft/,"W3School"))



範例程式碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>Javascript 字串對象</title><head>  <style>    body {background-color:#e6e6e6}  </style></head><body>  <h3>(一)length屬性:擷取字串的長度</h3>  <p id="hw">Hello world, Hello javascript!</p>  <script>    var s = document.getElementById("hw").innerHTML;    document.write("length:"+s.length);  </script>  <h3>(二)為字串添加樣式</h3>  <p>對字串調用樣式的相關方法時,會自動拼接相應的html標籤</p>  <p id = "hw_02">some words</p>  <button onclick="alertBig()">Call txt.big()</button>  <script>    var txt = document.getElementById("hw_02").innerHTML;    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>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")    function alertBig(){      alert(txt.big());    }  </script>  <h3>(三)indexOf方法:定位字串中某一個指定的字元首次出現的位置</h3>  <script>    var hw_text = "Hello world";    document.write(hw_text.indexOf("Hello")+"<br/>");    document.write(hw_text.indexOf("world")+"<br/>");    document.write(hw_text.indexOf("abc")+"<br/>");  </script>  <h3>(四)replace()方法:替換字串中的部分內容</h3>  <script>    var str="Visit Microsoft!"    document.write(str.replace(/Microsoft/,"W3School"))  </script></body></html>





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.