Split line suppressing all null fields

I want to split a line that suppresses all null fields

Team:

",1,2,,3,4,,".split(',')

Result:

["", "1", "2", "", "3", "4", ""]

Expected:

["1", "2", "3", "4"]

How to do it?


Edit

Ok Just to summarize all the good questions.

I would like the split method (or another method) to not generate blank lines. It seems like this is impossible.

So, the solution is a two-stage process: as usual, it splits the string, and then somehow removes the empty strings from the resulting array.

The second part is precisely this question (and its duplicate )

Therefore i would use

",1,2,,3,4,,".split(',').delete_if(&:empty?) 

, 229426, . docs reject . delete_if , . , , .

steenslag , split by space:

",1,2,,3,4,,".gsub(',', ' ').split(' ')

, , . "split (/\ s/)" "split ('')" . ?

- . , , . , . ! , , -- ,

",1,2, ,3 3,4 4 4,,".scan(/\w+[\s*\w*]*/)

:

["1", "2", "3 3", "4 4 4"]

, .

, split , . , . , .

.

+5
5

Array:

",1,2,,3,4,,".split(',').reject { |s| s.empty? }

, Symbol#to_proc:

",1,2,,3,4,,".split(',').reject(&:empty?)
+10

:

"split (/\ s/)" "split ('')" . ?

String # split, , '- :

If pattern is a single space, str is split on whitespace,
with leading whitespace and runs of contiguous whitespace characters ignored.

:

, . , .

, , .; -)

split , , , , . , csv Excel. , ",", , , .

, - , , !

, , , ?

a = ",1,2,,3,4,,5,,,,6,,,".split(/,+/)

:/, +/ , .

, , , . , split , ( String #):

If the limit parameter is omitted, trailing null fields are suppressed.

, , -, nil , . gsub :

a = ",1,2,,3,4,,5,,,,6,,,".gsub(/^,+/,'')

, , . :

a = ",1,2,,3,4,,5,,,,6,,,".gsub(/^,+/,'').split(/,+/)

!

, , , , , . , , :

a = ",1,2,,3,4,,5,,,,6,,,".gsub(/,+/,',').gsub(/^,/,'').split(',')

- . , , , , , , , - ( , , :)

, !

+2

split,

",1,2,,3,4,,".split(',').select{|x|!x.empty?}

, :

",1,2,,3,4,,".scan(/[^,]+/)
+1
",1,2,,3,4,,".split(/,/).reject(&:empty?)

",1,2,,3,,,4,,".squeeze(",").sub(/^,*|,*$/,"").split(",")
0

# split (pattern) , (ruby-doc).

",1,2,,3,4,,".gsub(',', ' ').split(' ')
0

All Articles