# Why is slice ignoring my fields at the end of the string?

**URL:** <https://www.ruby-forum.com/t/why-is-slice-ignoring-my-fields-at-the-end-of-the-string/76733>\
**Category:** Ruby\
**Created:** [November 20, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/why-is-slice-ignoring-my-fields-at-the-end-of-the-string/76733 "2006-11-20T21:27:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [November 20, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/why-is-slice-ignoring-my-fields-at-the-end-of-the-string/76733/1 "2006-11-20T21:27:53Z")

</div>

I’m not sure if this is a bug with the slice method, or if I’m just  
misunderstanding the logic behind it. If I have a string “1,2,3” that I  
slice into an array, I get 3 values.

But if I have “1,2,3,” with a trailing comma, I still get 3 – even  
though I probably should have gotten 4, with 4 being an empty string.

If I have “1,2,3” then I will get 4 elements with the third one  
properly being an empty string.

The trailing delimiter problem seems to repeat – if I have “1,2,3,”  
then I still end up with an array of just 3 elements. All of ones on  
the end are gone.

Anybody know if this is the intended behavior, and if so, why? I’d  
think that if I’m saying delimit fields on this character, then it would  
be up to me to decide whether or not to keep the empties. Maybe there’s  
a way to call split with a flag that says to keep the empties, I’m not  
sure. That just occurred to me, gonna go look into that.

D

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [November 20, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/why-is-slice-ignoring-my-fields-at-the-end-of-the-string/76733/2 "2006-11-20T21:29:04Z")

</div>

I wrote that entire post saying “slice” when what I meant to say was  
“split”.  
Splitting a string into an array using a specified delimiter.

Sorry about that!

D  
Duane M. wrote:

> I’m not sure if this is a bug with the slice method, or if I’m just  
> misunderstanding the logic behind it. If I have a string “1,2,3” that I  
> slice into an array, I get 3 values.
> 
> But if I have “1,2,3,” with a trailing comma, I still get 3 – even  
> though I probably should have gotten 4, with 4 being an empty string.
> 
> If I have “1,2,3” then I will get 4 elements with the third one  
> properly being an empty string.
> 
> The trailing delimiter problem seems to repeat – if I have “1,2,3,”  
> then I still end up with an array of just 3 elements. All of ones on  
> the end are gone.
> 
> Anybody know if this is the intended behavior, and if so, why? I’d  
> think that if I’m saying delimit fields on this character, then it would  
> be up to me to decide whether or not to keep the empties. Maybe there’s  
> a way to call split with a flag that says to keep the empties, I’m not  
> sure. That just occurred to me, gonna go look into that.
> 
> D

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [November 20, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/why-is-slice-ignoring-my-fields-at-the-end-of-the-string/76733/3 "2006-11-20T21:31:32Z")

</div>

Found it – there’s a “limit” parameter that can be passed to split. If  
the value is non-zero, that affects whether null fields are returned as  
part of the result. I don’t fully understand why that is (specifying -1  
says to give me all of my fields regardless of how many I have,  
including nulls), so I wasn’t exactly looking for such a thing.

d  
Duane M. wrote:

> I wrote that entire post saying “slice” when what I meant to say was  
> “split”.  
> Splitting a string into an array using a specified delimiter.
> 
> Sorry about that!
> 
> D  
> Duane M. wrote:
> 
> > I’m not sure if this is a bug with the slice method, or if I’m just  
> > misunderstanding the logic behind it. If I have a string “1,2,3” that I  
> > slice into an array, I get 3 values.
> > 
> > But if I have “1,2,3,” with a trailing comma, I still get 3 – even  
> > though I probably should have gotten 4, with 4 being an empty string.
> > 
> > If I have “1,2,3” then I will get 4 elements with the third one  
> > properly being an empty string.
> > 
> > The trailing delimiter problem seems to repeat – if I have “1,2,3,”  
> > then I still end up with an array of just 3 elements. All of ones on  
> > the end are gone.
> > 
> > Anybody know if this is the intended behavior, and if so, why? I’d  
> > think that if I’m saying delimit fields on this character, then it would  
> > be up to me to decide whether or not to keep the empties. Maybe there’s  
> > a way to call split with a flag that says to keep the empties, I’m not  
> > sure. That just occurred to me, gonna go look into that.
> > 
> > D

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [November 20, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/why-is-slice-ignoring-my-fields-at-the-end-of-the-string/76733/4 "2006-11-20T21:38:12Z")

</div>

On Tue, 21 Nov 2006, Duane M. wrote:

> I’m not sure if this is a bug with the slice method, or if I’m just  
> misunderstanding the logic behind it. If I have a string “1,2,3” that I  
> slice into an array, I get 3 values.

## OK, but using what code? ----------------------------------------------------------- String#slice […] str.slice(fixnum) =\> fixnum or nil str.slice(fixnum, fixnum) =\> new\_str or nil str.slice(range) =\> new\_str or nil str.slice(regexp) =\> new\_str or nil str.slice(regexp, fixnum) =\> new\_str or nil str.slice(other\_str) =\> new\_str or nil

[…]

```
 If a +Regexp+ is supplied, the matching portion of _str_ is
 returned. If a numeric parameter follows the regular expression,
 that component of the +MatchData+ is returned instead. If a
 +String+ is given, that string is returned if it occurs in _str_.
 In both cases, +nil+ is returned if there is no match.

```

I think you must be using /\d+/ or something.

Maybe what you want is split(/,/) ?

```
    [...]

```

> Anybody know if this is the intended behavior, and if so, why? I’d

You will need to show us the code.

> D

```
    Hugh
```
