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 split a comma-separated string?
You could do this:
String str = "...";
List<String> elephantList = Arrays.asList(str.split(","));
Basically the .split()
method will split the string according to (in this case) delimiter you are passing and will return an array of strings.
However, you seem to be after a List of Strings rather than an array, so the array must be turned into a list by using the Arrays.asList()
utility. Just as an FYI you could also do something like so:
String str = "...";
ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));
But it is usually better practice to program to an interface rather than to an actual concrete implementation, so I would recommend the 1st option.
How to split a comma-delimited string into an array of empty strings
You need to use the overloaded String#split(regex, limit)
method which takes in the limit parameter.
String[] tokens = str.split(",", -1);
From the docs(emphasis mine):
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
Explanation: When you do not provide the limit argument or provide "zero" as the limit, the split()
discards trailing empty fields. When you provide a positive limit argument, it limits the number of fields to that particular limit. But when you provide a negative limit, the split()
method allows any number of fields and also not discarding the trailing empty fields. To be more clear, have a look at the source code of the Pattern#split(regex, limit)
which has this snippet at the end(comments have been added by me and were not present in the actual source code).
if (limit == 0) // When zero or no arg is given
while (resultSize > 0 && matchList.get(resultSize-1).equals("")) // if trailing entries are blank
resultSize--; // remove them out
Note: If you do not provide any limit argument, the split()
method without limit argument calls the overloaded split()
method like this.
public String[] split(String regex) {
return split(regex, 0);
}
And also note that, String#split(regex, limit)
internally calls the Pattern#split(regex, limit)
.
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 comma-separated String to List?
Convert comma separated String to List
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace
which will place the words into the list and collapse any whitespace between the words and commas.
Please note that this returns simply a wrapper on an array: you CANNOT for example .remove()
from the resulting List
. For an actual ArrayList
you must further use new ArrayList<String>
.
How to split a string into an array in Bash?
IFS=', ' read -r -a array <<< "$string"
Note that the characters in $IFS
are treated individually as separators so that in this case fields may be separated by either a comma or a space rather than the sequence of the two characters. Interestingly though, empty fields aren't created when comma-space appears in the input because the space is treated specially.
To access an individual element:
echo "${array[0]}"
To iterate over the elements:
for element in "${array[@]}"
do
echo "$element"
done
To get both the index and the value:
for index in "${!array[@]}"
do
echo "$index ${array[index]}"
done
The last example is useful because Bash arrays are sparse. In other words, you can delete an element or add an element and then the indices are not contiguous.
unset "array[1]"
array[42]=Earth
To get the number of elements in an array:
echo "${#array[@]}"
As mentioned above, arrays can be sparse so you shouldn't use the length to get the last element. Here's how you can in Bash 4.2 and later:
echo "${array[-1]}"
in any version of Bash (from somewhere after 2.05b):
echo "${array[@]: -1:1}"
Larger negative offsets select farther from the end of the array. Note the space before the minus sign in the older form. It is required.
How to arrange comma separated string in Arrays of String in c?
You have several issues to work through to convert your function from simply printing the separated strings to stdout
to saving the separated strings in ArrayOfString
. Before getting to the changes, let's avoid using magic numbers in your code.
char ArrayOfString[10][5];
In ArrayOfString
above, 10
and 5
are magic numbers. They are hardcoded values that will govern everything from the declaration size to required validation checks to protect your array bounds. Instead of hardcoding values, if you need a constant, define one (or more), e.g.
#include <stdio.h>
#include <string.h>
#define ROW 10
#define COL 5
char ArrayOfString[ROW][COL];
Now on to separating your string into tokens. The C-library provides a function specifically tailored to separating delimited strings into tokens. Conveniently named strtok
. The only caveat to know about strtok
is that it modifies the string, so if you need to preserve the original, make a copy first.
To use strtok (string, delims)
to separate string
into tokens at delims
, your first call to strtok
takes string
as the 1st parameter. All subsequent calls use NULL
in its place. You can either make an initial call to strtok
and then use a while loop to complete the process, or a for
loop is tailor made for handling the initial call, as well as all subsequent calls with NULL
.
For example your function utilizing strtok
to separate string
into tokens and providing a size_t
return of the number of tokens copied to ArrayOfString
could be similar to:
char ArrayOfString[ROW][COL];
size_t vSeparateSringByComma (char* string)
{
const char *delims = ",\n";
char *s = string;
size_t n = 0, len;
for (s = strtok (s, delims); s && n < ROW; s = strtok (NULL, delims))
if ((len = strlen (s)) < COL)
strcpy (ArrayOfString[n++], s);
else
fprintf (stderr, "error: '%s' exceeds COL - 1 chars.\n", s);
return n;
}
(note: how your array bounds are protected both by the check with n < ROW
and each row array bound is protected with (len = strlen (s)) < COL
before the copy to ArrayOfString[n++]
is made)
(also note: how by not using magic numbers, if you change the ROW
or COL
size in the future, only the constants need changing and the change is automatically incorporated throughout your code by virtue of using constants)
Your example program would then be:
int main(void) {
char string[] = "$,0,3,307,183,18,5,119,1,#";
size_t n = vSeparateSringByComma (string);
for (size_t i = 0; i < n; i++)
printf ("ArrayOfString[%zu] : '%s'\n", i, ArrayOfString[i]);
}
Example Use/Output
$ ./bin/arrayofstrings
ArrayOfString[0] : '$'
ArrayOfString[1] : '0'
ArrayOfString[2] : '3'
ArrayOfString[3] : '307'
ArrayOfString[4] : '183'
ArrayOfString[5] : '18'
ArrayOfString[6] : '5'
ArrayOfString[7] : '119'
ArrayOfString[8] : '1'
ArrayOfString[9] : '#'
Using strcspn
and strspn
Instead of strtok
As discussed in the comments, using strcspn
to report the number of sequential characters not containing a delimiter allowing you to determined the length of each field. You then need to skip over the delimiters (which in many cases can be made up of more than one delimiter (e.g. $, 0, 3, ...
).
While strtok
considers multiple sequential delimiters a single delimiter, you would need a similar way to skip over the intervening delimiters to position your self to read the next field. strspn
will work nicely using the same delims
, but this time reporting the number of characters made up of only characters within delims
(allowing you to add that to your len
and then len
to s
to position yourself for the next read)
A short variation using strcspn
and strspn
could be:
size_t vSeperateSringByComma (char* string)
{
const char *delims = ",\n";
char *s = string;
size_t n = 0, len;
while ((len = strcspn (s, delims))) { /* number of non-delim chars */
if (len < COL) { /* validate it will fit */
memcpy (ArrayOfString[n], s, len); /* copy len chars */
ArrayOfString[n++][len] = 0; /* nul terminate at len */
}
else
fprintf (stderr, "error: '%s' exceeds COL - 1 chars.\n", s);
len += strspn (s + len, delims); /* scan past delimiter(s) */
s += len; /* update s to beginning of next field */
}
return n;
}
(the output is the same)
Look things over and let me know if you have further questions.
Split comma separated more than one strings and push into an array values
If you want to use LINQ:
var resultArray = a.Split(',').Concat(b.Split(',')).ToArray();
Or not using LINQ, you could do the concat as a string then split using one of these 3 lines:
var resultArray = $"{a},{b}".Split(','); //c#6+ String interpolation-formatted style
var resultArray = string.Format("{0},{1}", a, b).Split(','); //c# any version, formatted style
var resultArray = (a+","+b).Split(','); //concat style
Or you could load them into a list and then turn it into an array:
var l = new List<string>(a.Split(','));
l.AddRange(b.Split(','));
var resultArray = l.ToArray();
This is by no means an exhaustive list but it details the simplest ways of doing it with LINQ (easy if you have multiple different kinds of enumerables) , without LINQ (if it really is a pair of short strings and you want an easy to read snippet), with a collection (if you want to pass it around and fill it from different places)
If the scenario is truly as you have here, a couple of short strings, I would go with string concat then split. The string class has specific optimisations for the "concatenating 3 strings together" operation so it should be reasonably performance, short to code and easy to understand. If you're going to be doing millions of these ops then it may be faster to split to two arrays, make a third array that is as long as both a and b length arrays then copy a into the start and b in at offset a.Length
Related Topics
PHP Method Chaining or Fluent Interface
In_Array() and Multidimensional Array
How to Use PHP Serialize() and Unserialize()
When Should I Use Prepared Statements
Preferred Method to Store PHP Arrays (Json_Encode VS Serialize)
Laravel Requires the Mcrypt PHP Extension
Displaying HTML With Blade Shows the HTML Code
Strange Character Encoding of Stored Data , Old Script Is Showing Them Fine New One Doesn'T
Split a Comma-Delimited String into an Array
How to Search by Key=≫Value in a Multidimensional Array in PHP
How to Get a Variable Name as a String in PHP
Passing Base64 Encoded Strings in Url
How to Extract Data from CSV File in PHP
PHP Session Fixation/Hijacking
Https and Ssl3_Get_Server_Certificate:Certificate Verify Failed, Ca Is Ok