Convert string to array in JavaScript using split function and separator

The below example will convert the string to array by using a separator in JavaScript.
Split() is an inbuilt JavaScript function which return the array list, and it will take the separator as parameter.

Output of the example :



Source Code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Convert string to array using JavaScript split function.</title> <script language="javascript"> function SplitString() { var input = document.getElementById("txtinput").value; var separator = document.getElementById("txtseparator").value; var ary = input.split(separator); var div = document.getElementById("divres"); var stroutput = ""; if (div != null) { stroutput = "<table border='1'>"; for (var i = 0; i < ary.length; i++) { stroutput = stroutput + "<tr><td>" + ary[i] + "</td></tr>"; } stroutput = stroutput + "</table>"; } div.innerHTML = stroutput; } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> Enter Text </td> <td> Enter Separator </td> </tr> <tr> <td> <input type="text" id="txtinput" /> </td> <td> <input type="text" id="txtseparator" /> </td> <td> <input type="button" onclick="SplitString();" value="Split" /> </td> </tr> </table> </div> <div id="divres"> </div> </form> </body> </html>