Split Comma Delimited String

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.

Comma separated string split

Try:

a <- c("1,2,3","344")
scan(text = a, sep = ",", what = "")
# [1] "1" "2" "3" "344"

Split comma delimited string

strsplit gives you back a list of the character vectors, so if you want it in a single vector, use unlist as well.
So,

    unlist(strsplit(string, ","))

Split comma separated string and store into variables

Its as simple as :

var results = name.Split(',');

if(results.Length != 4)
throw new InvalidOperationException("Oh Noes!!!");

string fname = results[0];
string mname = results[1];
string lname = results[2];
string address= results[3];

Warning the second you have a comma in your address, this is going to fail.

If this is a CSV file, consider using a dedicated CSV Parser, there are plenty

Further reading

String.Split Method

Returns a string array that contains the substrings in this instance
that are delimited by elements of a specified string or Unicode
character array.

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).

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 comma-separated value to columns

CREATE FUNCTION [dbo].[fn_split_string_to_column] (
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @out_put TABLE (
[column_id] INT IDENTITY(1, 1) NOT NULL,
[value] NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @value NVARCHAR(MAX),
@pos INT = 0,
@len INT = 0

SET @string = CASE
WHEN RIGHT(@string, 1) != @delimiter
THEN @string + @delimiter
ELSE @string
END

WHILE CHARINDEX(@delimiter, @string, @pos + 1) > 0
BEGIN
SET @len = CHARINDEX(@delimiter, @string, @pos + 1) - @pos
SET @value = SUBSTRING(@string, @pos, @len)

INSERT INTO @out_put ([value])
SELECT LTRIM(RTRIM(@value)) AS [column]

SET @pos = CHARINDEX(@delimiter, @string, @pos + @len) + 1
END

RETURN
END

Split comma delimited string

strsplit gives you back a list of the character vectors, so if you want it in a single vector, use unlist as well.
So,

    unlist(strsplit(string, ","))

Split a comma separated string into defined number of pieces in R

We could split the text on comma (',') and divide them into group of 5.

temp <- strsplit(txt, ",")[[1]]
split(temp, rep(seq_along(temp), each = 5, length.out = length(temp)))

#$`1`
#[1] "120923" "120417" "120416" "105720" "120925"

#$`2`
#[1] "120790" "120792" "120922" "120928" "120930"

#$`3`
#[1] "120918" "120929" "61065" "120421"

If you want them as one concatenated string we can use by

as.character(by(temp, rep(seq_along(temp), each  = 5, 
length.out = length(temp)), toString))

Split Comma delimited String excluding those in brackets within the brackets

If you want to select the comma when and what is at the right should be 2 times an opening and 2 times a closing square bracket, you might use:

,(?=\[[^[]*\[[^[]*\][^]]*\])

In Java:

String regex = ",(?=\\[[^\\[]*\\[[^\\[]*\\][^]]*\\])";

See the Regex demo | Java demo

That will match:

  • , Match comma
  • (?= Positive lookahead

    • \[[^[]*\[[^[]*\][^]]+\] matches:
    • \[ Match [
    • [^[]* Negated character class not matching [
    • \[ Match [
    • [^[]* Negated character class not matching [
    • \] Match ]
    • [^]]* Negated character class not matching ]
    • \] Match ]
  • ) Close positive lookahead


Related Topics



Leave a reply



Submit