How to split string and remove empty elements from the array in Ruby

1 Answer

0 votes
s = "ruby,java,,c++,,,python"
 
arr = s.split(",")

print arr, "\n"

arr.delete_if{|element| element.length == 0}

print arr, "\n"

 
 
 
# run:
#
# ["ruby", "java", "", "c++", "", "", "python"]
# ["ruby", "java", "c++", "python"]
#

 



answered Nov 7, 2020 by avibootz

Related questions

2 answers 366 views
1 answer 239 views
1 answer 236 views
1 answer 237 views
1 answer 191 views
...