How can I convert a comma-separated string to an array?
var array = string.split(',');
MDN reference, mostly helpful for the possibly unexpected behavior of the limit
parameter. (Hint: "a,b,c".split(",", 2)
comes out to ["a", "b"]
, not ["a", "b,c"]
.)
Convert string with commas to array
For simple array members like that, you can use JSON.parse
.
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
[0, 1]
If you use .split()
, you'll end up with an Array of strings.
["0", "1"]
Just be aware that JSON.parse
will limit you to the supported data types. If you need values like undefined
or functions, you'd need to use eval()
, or a JavaScript parser.
If you want to use .split()
, but you also want an Array of Numbers, you could use Array.prototype.map
, though you'd need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Converting a comma separated string into an array
Modify the string so that it's valid JSON, then JSON.parse()
it:
var str = '"alpha", "beta", "gamma", "delta"';var json = '[' + str + ']';var array = JSON.parse(json);
console.log(array[0])
How to convert comma-delimited string to list in Python?
You can use the str.split method.
>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']
If you want to convert it to a tuple, just
>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')
If you are looking to append to a list, try this:
>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']
String with Comma separated numbers to array of integer in javascript
I would recommend this:
var array;
if (string.length === 0) {
array = new Array();
} else {
array = string.replace(/, +/g, ",").split(",").map(Number);
}
How to convert a string of comma separated numbers to integers?
First split the values with comma using the split()
method like this.
arr.split(',')
After that you will get each value separated in an array which will be accessible by giving array index. So arr[0]
will include 108 and arr[1]
will include 109. Now all that's left to do is parse them individually.
parseInt(arr[0])
parseInt(arr[1])
How to convert array into comma separated string in javascript
The method array.toString()
actually calls array.join()
which result in a string concatenated by commas. ref
var array = ['a','b','c','d','e','f'];document.write(array.toString()); // "a,b,c,d,e,f"
How do I convert a comma-separated string into an array?
Use the split
method to do it:
"one,two,three,four".split(',')
# ["one","two","three","four"]
If you want to ignore leading / trailing whitespace use:
"one , two , three , four".split(/\s*,\s*/)
# ["one", "two", "three", "four"]
If you want to parse multiple lines (i.e. a CSV file) into separate arrays:
require "csv"
CSV.parse("one,two\nthree,four")
# [["one","two"],["three","four"]]
Related Topics
How to Fire and Forget a Promise in Nodejs (Es7)
How to Sort 2 Dimensional Array by Column Value
How to Capture an Arbitrary Number of Groups in JavaScript Regexp
How to Use Namespaces with Typescript External Modules
Detecting Arrow Key Presses in JavaScript
Copy Array Items into Another Array
How to Dynamically Change Header Based on Angularjs Partial View
Const in JavaScript: When to Use It and Is It Necessary
Angularjs Passing Data to $Http.Get Request
Why Do We Use Spreadsheetapp.Flush();
What's Wrong with Awaiting a Promise Chain
Determining Image File Size + Dimensions via JavaScript
How to Access the First Property of a JavaScript Object
In JavaScript, Why Is "0" Equal to False, But When Tested by 'If' It Is Not False by Itself
Check Whether a String Matches a Regex in Js
Using Rails 3.1, Where Do You Put Your "Page Specific" JavaScript Code