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 8140395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:09:04+00:00 2026-06-06T12:09:04+00:00

In Ruby 1.9.3 (and probably earlier versions, not sure), I’m trying to figure out

  • 0

In Ruby 1.9.3 (and probably earlier versions, not sure), I’m trying to figure out why Ruby’s String#split method is giving me certain results. The results I’m getting seem counter-intuitive from what I would expect. Here’s an example:

"abcabc".split("b") #=> ["a", "ca", "c"]
"abcabc".split("a") #=> ["", "bc", "bc"]
"abcabc".split("c") #=> ["ab", "ab"]

Here, the first example returns exactly what I would expect.

But in the second example, I’m confused as to why #split is returning a zero-length string as the first value of the returned array. What is the reasoning for this? This is what I would expect instead:

"abcabc".split("a") #=> ["bc", "bc"]

And along that same line, how come there is no trailing zero-length string returned in the third example? If the second example returns a zero-length string as it’s first value, then the last example should return the same as it’s last value.

Enlighten me, what am I missing here??

EDIT: Looking more into it, I realize why this is the default behavior and why my thinking is completely wrong. If we were to go through a CSV file for example, splitting on each column, our data would be thrown off because empty leading columns would be ignored.

Also it’s important to note that this question isn’t related to only Ruby–I’m learning that many other languages behave in the exact same manner. I was simply using Ruby when I learned of this.

  • 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-06-06T12:09:05+00:00Added an answer on June 6, 2026 at 12:09 pm
    "abcabc".split("b") #=> ["a", "ca", "c"]
    "abcabc".split("a") #=> ["", "bc", "bc"]
    "abcabc".split("c") #=> ["ab", "ab"]
    

    Suppose you were splitting on a comma. What behaviour would you expect from
    ",bc,bc".split(',')? It’s not different with splitting on ‘a’. For the third example, split omits the trailing empties by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that I'm probably not writing lazily instantiated methods/attributes in the most ruby
I'm implementing Schema microformats on a Ruby project using HAML and can't figure out
Possible Duplicate: What is the * operator doing to this string in Ruby Probably
Ruby has a select method that takes an array and returns a subarray consisting
This is probably very simple, but I'm quite new to ruby and active record.
I'm a beginner of Ruby and Rails, so this is probably an easy question.
I am new to Ruby On Rails, and currently trying to modify an existing
UPDATE: I probably messed up my system while trying solutions found somewhere else. I
I am very beginner in Ruby and probably the question is too easy but
Using Perl, Python, or Ruby, can I write a program, probably calling Win32 API,

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.