您好,登錄后才能下訂單哦!
本文實例講述了JavaScript中join()、splice()、slice()和split()函數用法。分享給大家供大家參考,具體如下:
join()
join()
方法用于把數組中的所有元素放入一個字符串。
元素是通過指定的分隔符進行分隔的。
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write( arr.join() ) </script>
運行結果:
George,John,Thomas
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write( arr.join(".") ) </script>
運行結果:
George.John.Thomas
split()
split()
方法用于把一個字符串分割成字符串數組。
<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>
運行結果:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
"2:3:4:5".split(":") //將返回["2", "3", "4", "5"] "|a|b|c".split("|") //將返回["", "a", "b", "c"] "hello".split("") //可返回 ["h", "e", "l", "l", "o"] "hello".split("", 3) //可返回 ["h", "e", "l"]
slice()
slice()
方法可從已有的數組中返回選定的元素。
<script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write( arr.slice(1) + "<br />") document.write(arr) </script>
運行結果:
George,John,Thomas
John,Thomas
George,John,Thomas
<script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") document.write( arr.slice(2,4) + "<br />") document.write(arr) </script>
運行結果:
George,John,Thomas,James,Adrew,Martin
Thomas,James
George,John,Thomas,James,Adrew,Martin
splice()
splice()
方法向/從數組中添加/刪除項目,然后返回被刪除的項目。
注釋:該方法會改變原始數組。
<script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") arr.splice(2,0,"William") document.write(arr + "<br />") </script>
運行結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,Thomas,James,Adrew,Martin
<script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") arr.splice(2,1,"William") document.write(arr) </script>
運行結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,James,Adrew,Martin
<script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") arr.splice(2,3,"William") document.write(arr) </script>
運行結果:
George,John,Thomas,James,Adrew,Martin
George,John,William,Martin
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試運行一下文中所述代碼。
更多關于JavaScript相關內容可查看本站專題:《JavaScript常用函數技巧匯總》、《JavaScript字符與字符串操作技巧總結》、《javascript面向對象入門教程》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》及《JavaScript數組操作技巧總結》
希望本文所述對大家JavaScript程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。