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"]
.)
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])
Split a comma-delimited string into an array?
Try explode:
$myString = "9,admin@example.com,8";
$myArray = explode(',', $myString);
print_r($myArray);
Output :
Array
(
[0] => 9
[1] => admin@example.com
[2] => 8
)
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 to create convert a comma separated string into a string array in C
Use strtok like the following... arrayOfString will have list of strings.
#include <string.h>
#include <stdio.h>
int main()
{
char str[80] = "apples, pears, oranges,";
char* arrayOfString[3];
const char s[2] = ", ";
char *token;
int i=0;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL )
{
arrayOfString[i] = token;
token = strtok(NULL, s);
i++;
}
for(i=0;i<3;i++)
printf("%s\n", arrayOfString[i]);
return(0);
}
Convert comma separated string to an array with Powershell
I'm not quite sure I understood the premise of the question but, what you're looking for might be accomplished by hardcoding the CSV in the code itself, this would solve the "but I want to simplify it for others to edit a simply comma separated list versus creating all net new variables.".
So for this you can use a here-string combined with ConvertFrom-Csv
:
@'
"name","source","destination","timing"
"objects","\\server\folder\a","/sftp/objects/","4"
"items","\\server\folder\a","/sftp/items/","4"
'@ | ConvertFrom-Csv | ForEach-Object {
$transferResult = $session.GetFiles($_.source, ($_.destination + $_.name), $true, $transferOptions)
$transferResult.Check()
Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of $($_.FileName) succeeded" -Category 1 -RawData 10,20
}
Technically you could use a hardcoded Json too, but adding new objects to process may be harder in this case. This method uses ConvertFrom-Json
:
@'
[
{
"name": "objects",
"source": "\\\\server\\folder\\a",
"destination": "/sftp/objects/",
"timing": "4"
},
{
"name": "items",
"source": "\\\\server\\folder\\a",
"destination": "/sftp/items/",
"timing": "4"
}
]
'@ | ConvertFrom-Json | ForEach-Object {
$transferResult = $session.GetFiles($_.source, ($_.destination + $_.name), $true, $transferOptions)
$transferResult.Check()
Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of $($_.FileName) succeeded" -Category 1 -RawData 10,20
}
What's the most simple way to convert comma separated string to int[]?
string s = "1,5,7";
int[] nums = Array.ConvertAll(s.Split(','), int.Parse);
or, a LINQ-y version:
int[] nums = s.Split(',').Select(int.Parse).ToArray();
But the first one should be a teeny bit faster.
Related Topics
How to Update Ruby with Homebrew
Rails, How to Render a View/Partial in a Model
How to Log Every Method That's Called in a Ruby Program
In Ruby, What Is the Cleanest Way of Obtaining the Index of the Largest Value in an Array
How to Convert Array of Activerecord Models to CSV
Where to Place/Access Config File in Gem
Factory Girl - What's the Purpose
Upgrading from Rails 3 to Rails 3.1
When We Import CSV Data, How Eliminate "Invalid Byte Sequence in Utf-8"
Cannot Access Local Sinatra Server from Another Computer on Same Network
Ruby Convert Array into Function Arguments
How to Get Activerecord Associations via Reflection
Rails 4.0.1 on Heroku, Can't Create Database