split() 方法用于把一個字符串分割成字符串?dāng)?shù)組。
語法:
stringObject.split(separator,howmany)
參數(shù)
描述
separator 必需。字符串或正則表達(dá)式,從該參數(shù)指定的地方分割 stringObject。
howmany 可選。該參數(shù)可指定返回的數(shù)組的最大長度。如果設(shè)置了該參數(shù),返回的子串不會多于這個參數(shù)指定的數(shù)組。如果沒有設(shè)置該參數(shù),整個字符串都會被分割,不考慮它的長度。
返回值:
一個字符串?dāng)?shù)組。該數(shù)組是通過在 separator 指定的邊界處將字符串 stringObject 分割成子串創(chuàng)建的。返回的數(shù)組中的字串不包括 separator 自身。
但是,如果 separator 是包含子表達(dá)式的正則表達(dá)式,那么返回的數(shù)組中包括與這些子表達(dá)式匹配的字串(但不包括與整個正則表達(dá)式匹配的文本)。
實例1:
<html> <head> <title>使用指定的字符分割字符串</title> </head> <body> <script language="javascript"> <!-- name = "張三,李四,王五"; ch = new Array; ch = name.split(","); for(i=0;i<ch.length;i++){ document.write(ch[i],"<br>"); } //--> </script> </body> </html>
實例2:
<script type="text/javascript"> var str="How are you doing today?" document.write(str.split(" ") + "<br />") document.write(str.split("") + "<br />") document.write(str.split(" ",3)) </script>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com