<<< Js中實現對字串的截取

來源:互聯網
上載者:User

標籤:c   style   a   tar   color   strong   

1、函數split()

功能:使用一個指定的分隔字元把一個字串分割儲存到數組

 

例:var Data = "1/234/5678/9";

  var str = shuju.split("/")

  結果:1,234,5678,9

 

2、函數join()

功能:使用您選擇的分隔字元將一個數組合并為一個字串

例:var Data = new Array("1","2","3","4","5");

  var str = Data.join("-");

  結果:1-2-3-4-5

 

3、函數indexOf()

功能:返回字串中匹配子串的第一個字元的下標

例:var Data = "SevenNight";

  var str = Data.indexOf("en");  結果:3

  var str = Data.indexOf("v");  結果:2

  var str = Data.indexOf("jjj");  結果:-1     註:如果沒有匹配的字串,返回-1

 

4、LastIndexOf()

功能:指定字元在此執行個體中的最後一個匹配項的索引位置

例:var Data = "SevenNight";

  var str = Data.lastIndexOf("N");

  結果:5

 

5、方法substring()

功能:方法用於提取字串中介於兩個指定下標之間的字元

例:var Data = "SevenNight";

  var str = Data.substring(5);  結果:Night    註:只傳入一個下標返回的是此下標後面的所有字元

  var str = Data.substring(0,5); 結果:Seven  註:返回0至5中間的字元

  註:substring 方法返回的子串包括開始下標處的字元,但不包括結束下標處的字元

  如果開始下標與結束下標相等,那麼該方法返回的就是一個空串(即長度為 0 的字串)。

  如果開始下標比結束下標大,那麼該方法在提取子串之前會先交換這兩個參數。

  如果開始下標或結束下標為負數,那麼它將被替換為 0。

 

6、方法substr() 

功能:用於返回一個從指定位置開始的指定長度的子字串

用法:str.substr(startIndex,[length]);  //startIndex開始下標,length長度

例:var Data = "SevenNight";

  var str = Data.substr(5);  結果:Night   直接傳入開始下標的

  註:開始下標是必須的,length可選,length在返回的子字串中包括的字元個數

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

聯繫我們

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