Convert Comma Separated String Into An Array Example Using JavaScript


Share:
Published By: Admin, Published On: Nov 12, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to convert comma seprated string into array example. This is a short guide on convert comma seprated string into array. Let's get started with how to convert comma seprated string into array in javascript.

Here i will give you many example how you can convert comma seprated string into array javascript.

Example : 1
<!DOCTYPE html>   
<html>     
    <head>   
        <title>Convert Comma Separated String Into An Array Example Using JavaScript - MyWebtuts.com</title>   
    </head>   
    <body>   
        <h3>Convert Comma Separated String Into An Array Example Using JavaScript - MyWebtuts.com</h3>   
        <p>Original string is "One, Two, Three, Four, Five"</p>   
        <p>The values of the Separated Array is : <span class="output"></span></p>     
        <button onclick="seprateStr()">Remove Text</button>

        <script type="text/javascript">   
            function seprateStr() {   
                originalStr = "One, Two, Three, Four, Five";   
                separatedArray = originalStr.split(', ');     
                console.log(separatedArray);   
                document.querySelector('.output').textContent = separatedArray;   
            }   
        </script>   
    </body>   
</html> 
Output :
Original string is "One, Two, Three, Four, Five"
The values of the Separated Array is : One,Two,Three,Four,Five
Example : 2
<!DOCTYPE html>   
<html>  
    <head>   
        <title>Convert Comma Separated String Into An Array Example Using JavaScript - MyWebtuts.com</title>  
    </head>     
    <body>   
        <h3>Convert Comma Separated String Into An Array Example Using JavaScript - MyWebtuts.com</h3>  
        <p>Original string is "One, Two, Three, Four, Five"</p> 
        <p>Separated Array is: <span class="output"></span></p>   
        <button onclick="separateString()">Remove Text</button>   
        
        <script type="text/javascript">   
            function separateString()   
            {               
                originalString = "One, Two, Three, Four, Five";   
                separatedArray = [];   

                let previousIndex = 0;
                for (i = 0; i < originalString.length; i++)   
                {   
                    if (originalString[i] == ', ')
                    {                            
                        separated = originalString.slice(previousIndex, i);   
                        separatedArray.push(separated);   

                        previousIndex = i + 1;    
                    }   
                }  
                separatedArray.push(originalString.slice(previousIndex, i));
                console.log(separatedArray);   
                document.querySelector('.output').textContent = separatedArray;   
            }   
        </script>   
    </body>   
</html>
Output :
Original string is "One, Two, Three, Four, Five"
Separated Array is: One, Two, Three, Four, Five

It will help you...

Share: