Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:04:50+00:00 2026-05-17T00:04:50+00:00

I want to split a string suppressing all null fields Command: ,1,2,,3,4,,.split(‘,’) Result: [,

  • 0

I want to split a string suppressing all null fields

Command:

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

Result:

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

Expected:

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

How to do this?


Edit

Ok. Just to sum up all that good questions posted.

What I wanted is that split method (or other method) didn’t generate empty strings. Looks like it isn’t possible.

So, the solution is two step process: split string as usual, and then somehow delete empty strings from resulting array.

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

So I would use

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

The solution proposed by Nikita Rybak and by user229426 is to use reject method. According to docs reject returns a new array. While delete_if method is more efficient since I don’t want a copy. Using select proposed by Mark Byers even more inefficient.

steenslag proposed to replace commas with space and then use split by space:

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

Actually, the documentation says that space is actually a white space. But results of “split(/\s/)” and “split(‘ ‘)” are not the same. Why’s that?

Mark Byers proposed another solution – just using regular expressions. Seems like this is what I need. But this solution implies that you have to be a master of regexp. But this is great solution! For example, if I need spaces to be separators as well as any non-alphanumeric symbol I can rewrite this to

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

the result is:

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

But again regexps are very unintuitive and they need an experience.

Summary

I expect that split to work with whitespaces as if whitespaces were a comma or even regexp. I expect it to do not produce empty strings. I think this is a bug in ruby or my misunderstanding.

Made it a community question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:04:51+00:00Added an answer on May 17, 2026 at 12:04 am

    There’s a reject method in Array:

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

    Or if you prefer Symbol#to_proc:

    ",1,2,,3,4,,".split(',').reject(&:empty?)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split string by setting all non-alphabet as separator. String[] word_list =
I want to split a string like this: abc//def//ghi into a part before and
I want to split a string with all non-alphabetic characters as delimiters. For example,
I want to split a string in C#.NET that looks like this: string Letters
I want to split a string that can look like this: word1;word2;word3,word4,word5,word6.word7. etc. The
I want to split following string with a comma. 1,x1,43,tr,y,7 Result array should be
String filterPath=aa.bb.cc{k1:v1,k2:{s1:s2}},bb.cc,ee.dd; String[] result=filterPath.split(,); for(String r:result){ System.out.println(r); } I want split the String filterPath
i want to split this string by the (*) sign $string= How Many Papers*On
I want to split this string up into parts like Source: Web, Pics: 1
v=vi nod-u i want to split this string to obtain l=[vi],[nod],[u] l.split( ) splits

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.