How to split a comma separated string and process in a loop using JavaScript

Javascript

Javascript Problem Overview


How to split a comma separated string and process in a loop using JavaScript?

Javascript Solutions


Solution 1 - Javascript

My two cents, adding trim to remove the initial whitespaces left in sAc's answer.

var str = 'Hello, World, etc';
var str_array = str.split(',');

for(var i = 0; i < str_array.length; i++) {
   // Trim the excess whitespace.
   str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
   // Add additional code here, such as:
   alert(str_array[i]);
}

Edit:

After getting several upvotes on this answer, I wanted to revisit this. If you want to split on comma, and perform a trim operation, you can do it in one method call without any explicit loops due to the fact that split will also take a regular expression as an argument:

'Hello, cruel , world!'.split(/\s*,\s*/);
//-> ["Hello", "cruel", "world!"]

This solution, however, will not trim the beginning of the first item and the end of the last item which is typically not an issue.

And so to answer the question in regards to process in a loop, if your target browsers support ES5 array extras such as the map or forEach methods, then you could just simply do the following:

myStringWithCommas.split(/\s*,\s*/).forEach(function(myString) {
    console.log(myString);
});

Solution 2 - Javascript

Like this:

var str = 'Hello, World, etc';
var myarray = str.split(',');

for(var i = 0; i < myarray.length; i++)
{
   console.log(myarray[i]);
}

Solution 3 - Javascript

Try the following snippet:

var mystring = 'this,is,an,example';
var splits = mystring.split(",");
alert(splits[0]); // output: this

Edit:

The following snippet will allow you to split, manipulate each element and get the results in an array:

const string = "this,is,a,string";
const array = string.split(",")
  .map((item, i) => `Item ${i} => ${item}`);
console.log(array)

Solution 4 - Javascript

Please run below code may it helps you :)

var str = "this,is,an,example";
var strArr = str.split(',');
var data = "";
for(var i=0; i<strArr.length; i++){
  data += "Index : "+i+" value : "+strArr[i]+"<br/>";
}
document.getElementById('print').innerHTML = data;

<div id="print">
</div>

Solution 5 - Javascript

you can Try the following snippet:

var str = "How are you doing today?";
var res = str.split("o");
console.log("My Result:",res)

and your output like that

My Result: H,w are y,u d,ing t,day?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDEVOPSView Question on Stackoverflow
Solution 1 - JavascriptkzhView Answer on Stackoverflow
Solution 2 - JavascriptSarfrazView Answer on Stackoverflow
Solution 3 - JavascriptAnaxView Answer on Stackoverflow
Solution 4 - JavascriptRenish GotechaView Answer on Stackoverflow
Solution 5 - JavascriptAakash SajjadView Answer on Stackoverflow