Monday 3 September 2018

JavaScript: split: get an array from string

split method is used to split the string by using given separator and retun an array of elements.

Example
string.split(separator);

array.html
<!DOCTYPE html>

<html>

<head>
    <title>Array</title>
</head>

<body>
    <script type="text/javascript">
        var str = "1:2:3:4:5:6:7"
        var arr = str.split(":");

        for (i = 0; i < arr.length; i++) {
            document.write(arr[i] + " ");
        }
    </script>
</body>

</html>


Open above page in browser, you can able to see following content.
1 2 3 4 5 6 7




Previous                                                 Next                                                 Home

No comments:

Post a Comment