JavaScript: indexOf 詳解、執行個體與應用

來源:互聯網
上載者:User

JavaScript indexOf 

   返回 String 對象中第一次出現的子字串的字元位置。

 JavaScript indexOf格式

   strObj.indexOf(subString, start)

  參數 strObj 必填。String 對象或文字。

   subString 必填。indexOf函數要在 String 對象中尋找的子字串。

   start 可填。該整數值指出在 String 對象內開始尋找的索引。如果省略,則從字串第一個字元處尋找。

 注意:

  indexOf 方法返回一個整數值,指出 String 對象內子字串的開始位置。如果沒有找到子字串,則返回 -1。 JavaScript indexOf函數是區別大小寫。
 
 程式碼範例:

 1<script type="text/javascript">
 2    var str1 = "Javascript indexOf";
 3    var s = str1.indexOf("Javascript"); 
 4 var s1= str1.indexOf("IndexOf");
 5 var s2= str1.indexOf("indexOf");
 6 document.write("Javascript   在字串  Javascript indexOf  裡的位置是  <font color=red>");
 7 document.write(s);
 8 document.write("</font>  位<br>");
 9 
10 document.write("IndexOf   在字串  Javascript indexOf  裡的位置是  <font color=red>");
11 document.write(s1);
12 document.write("</font>  位<br>");
13 
14 document.write("indexOf   在字串  Javascript indexOf  裡的位置是  <font color=red>");
15 document.write(s2);
16 document.write("</font>  位<br>");
17</script>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.