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"]
.)
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 can I convert an array into a comma separated string?
There are a few ways to do this:
1. Join directly with printf
(via Charles Duffy’s comment)
printf -v joined '%s,' "${data[@]}"
echo "${joined%,}"
The printf
builtin implicitly joins arrays. You could print interactively like 3a below with a one-liner reading printf '%s,' "${data[@]}"
, but you'd be left with a trailing comma. (This method even works in POSIX shell, though you'd have to use $@
as your array since POSIX can't handle other array types).
2. Change the $IFS
field separator (via chepner’s answer)
join_arr() {
local IFS="$1"
shift
echo "$*"
}
join_arr , "${data[@]}"
This redefines the field separator within just the scope of this function so when the $data
array is automatically expanded, it uses the desired delimiter instead of the first value of the global $IFS
or (if it's empty or undefined) space.
This could be done without a function, but there's some nastiness about preserving $IFS
: Charles Duffy notes that reverting IFS="$OLD_IFS"
after temporarily reassigning it could evaluate to IFS=""
, but if $IFS
was previously undefined, that's different from unset IFS
and while it's possible to tease those apart, this functional approach is far cleaner thanks to its use of local
to limit $IFS
’s scope.
This solution only supports single-character delimiters. See #5 below for a similar function that supports delimiters of any length.
3a. Loop through its contents (and print incrementally)
delim=""
for item in "${data[@]}"; do
printf "%s" "$delim$item"
delim=","
done
echo # add a newline
If other code in that loop involves an external call (or even sleep 0.1
), you'll actually watch this build piece by piece, which can be helpful in an interactive setting.
3b. Loop through its contents (and build a variable)
delim=""
joined=""
for item in "${data[@]}"; do
joined="$joined$delim$item"
delim=","
done
echo "$joined"
4. Save the array as a string and run replacement on it (note, the array must lack spaces*)
data_string="${data[*]}"
echo "${data_string//${IFS:0:1}/,}"
* This will only work if the first character of $IFS
(space by default) does not exist in any of the array's items.
This uses bash pattern substitution: ${parameter//pattern/string}
will replace each instance of pattern
in $parameter
with string
. In this case, string
is ${IFS:0:1}
, the substring of $IFS
starting at the beginning and ending after one character.
Z Shell (zsh
) can do this in one nested parameter expansion:
echo "${${data[@]}//${IFS:0:1}/,}"
(Though Z Shell can also do it more elegantly with its dedicated join
flag as echo "${(j:,:)data}"
as noted by @DavidBaynard in a comment below this answer.)
5. Join with replacement in an implicit loop (via Nicholas Sushkin's answer to a duplicate question)
join_by() {
local d="${1-}" f="${2-}"
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
join_by , "${data[@]}"
This is very similar to #2 above (via chepner), but it uses pattern substitution rather than $IFS
and therefore supports multi-character delimiters. $d
saves the delimiter and $f
saves the first item in the array (I'll say why in a moment). The real magic is ${@/#/$d}
, which replaces the beginning (#
) of each array element with the delimiter ($d
). As you don't want to start with a delimiter, this uses shift
to get past not only the delimiter argument but also the first array element (saved as $f
), which is then printed right in front of the replacement.
printf
has an odd behavior when you give it extra arguments as we do here. The template (%s
) only specifies that there will be one argument, so the rest of the arguments act as if it's a loop and they're all concatenated onto each other. Consider changing that key line to printf "%s\n" "$f" "${@/#/$d}"
. You'll end up with a newline after each element. If you want a trailing newline after printing the joined array, do it with printf %s "$f" "${@/#/$d}" $'\n'
(we need to use the $'…'
notation to tell bash to interpret the escape; another way to do this would be to insert a literal newline, but then the code looks weird).
Convert array into comma separated strings
You could stringify the elements before joining.
var array = ["123", "456", "789"], result = array.map(a => JSON.stringify(a)).join(); console.log(result);
Convert Array of Strings to Comma Separated String with additional concatenation
Is there any simple Linq (one line expression) to do both.
string.Join(",", data.Select(item => "'" + item + "'"))
Basics of Linq: Transforms are Select
statements. Filters are Where
statements.
That said, there are a lot of string manipulation tools available that aren't Linq, and they're more likely to be optimized for strings, so I'd always look to them before looking to Linq.
How to convert string array to string by keeping the contents of array as comma separated values in MongoDB 4.0
You can achieve this with the following aggregation :
db.collection.aggregate([
{
$addFields: {
"availableProducts": {
$reduce: {
input: "$availableProducts",
initialValue: "",
in: {
"$cond": {
if: {
"$eq": [
{
"$indexOfArray": [
"$availableProducts",
"$$this"
]
},
0
]
},
then: {
"$concat": [
"$$value",
"$$this"
]
},
else: {
"$concat": [
"$$value",
",",
"$$this"
]
}
}
}
}
}
}
},
{$out:'collection'}
])
Use it carefully, as the $out stage will replace your entire collection with the result. My advice is to test without $out stage before.
You can test it here
How do I convert an array of strings into a comma-separated string?
["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'
Easy way to turn JavaScript array into comma-separated list?
The Array.prototype.join() method:
var arr = ["Zero", "One", "Two"];
document.write(arr.join(", "));
Convert an array of Ints to a comma separated string
Well you can do it like :
let formattedArray = ([0,1,1,0].map{String($0)}.joined(separator: ",")
Related Topics
What Does ':Location => ...' and 'Head :Ok' Mean in the 'Respond_To' Format Statement
Jekyll on Windows: Pygments Not Working
Rails 3 - Restricting Formats for Action in Resource Routes
Rails Rbenv: Rails: Command Not Found
Ruby 1.9, Force_Encoding, But Check
Rails - Invalid Authenticity Token After Deploy
Reraise (Same Exception) After Catching an Exception in Ruby
How to Access Url Helper from Rails Module
How to Describe an Enumeration Column in a Rails 3 Migration
How to Find the Path a Ruby Gem Is Installed at (I.E. Gem.Lib_Path C.F. Gem.Bin_Path)
Is There a Way in Ruby/Rails to Execute Code That Is in a String
Puppet/Facter "Could Not Retrieve Fact Fqdn": How to Fix or Circumvent
How to Call Java API from Ruby 1.8 or 1.9