How to Convert a Comma-Separated String into an Array

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



Leave a reply



Submit