# Splitting a string

**URL:** <https://rubytalk.org/t/splitting-a-string/38697>\
**Category:** ruby-talk\
**Created:** [28 June 2007 11:51 UTC](https://rubytalk.org/t/splitting-a-string/38697 "2007-06-28T11:51:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars\_Andren](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@Lars\_Andren](https://rubytalk.org/u/Lars_Andren)\
**Post date:** [28 June 2007 11:51 UTC](https://rubytalk.org/t/splitting-a-string/38697/1 "2007-06-28T11:51:49Z")

</div>

I've got a problem when trying to split a string after the thrid  
occurrence of a slash ('/'), I'm so used to java-thinking so I keep  
wanting to iterate through it and save the position of the third  
occurence and then cut the string at that index.

This doesnt seem to be the Ruby-way, anyone got ideas of how I should do  
it?  
Grateful for any assistance,  
Lars

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [28 June 2007 12:11 UTC](https://rubytalk.org/t/splitting-a-string/38697/2 "2007-06-28T12:11:56Z")

</div>

Alle giovedì 28 giugno 2007, Lars Andren ha scritto:

> I've got a problem when trying to split a string after the thrid  
> occurrence of a slash ('/'), I'm so used to java-thinking so I keep  
> wanting to iterate through it and save the position of the third  
> occurence and then cut the string at that index.
> 
> This doesnt seem to be the Ruby-way, anyone got ideas of how I should do  
> it?  
> Grateful for any assistance,  
> Lars

There may be better solutions, but this should work:

str.scan(/(?:[^\/]+(?:\/|$)){3}/)

This returns an array of strings obtained by splitting str after every  
third /. The last entry of the array contains the rest of the string:

'a/b/cd/efg/h/i'.scan(/(?:[^\/]+(?:\/|$)){3}/)  
=\> ["a/b/cd/", "efg/h/i"]

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 June 2007 12:25 UTC](https://rubytalk.org/t/splitting-a-string/38697/3 "2007-06-28T12:25:01Z")

</div>

Depends on what you want to do, i.e. which part(s) of the string you need. You can do

irb(main):003:0\> s = (1..5).to\_a.join '/'  
=\> "1/2/3/4/5"  
irb(main):008:0\> %r\<\A((?:[^/]\*/){2}[^/]\*)/(.\*)\z\> =~ s  
=\> 0  
irb(main):009:0\> pre, post = $1, $2  
=\> ["1/2/3", "4/5"]  
irb(main):010:0\>

Or

irb(main):011:0\> s[%r\<\A((?:[^/]\*/){2}[^/]\*)/\>]  
=\> "1/2/3"

Or...

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 28.06.2007 13:51, Lars Andren wrote:
> 
> > I've got a problem when trying to split a string after the thrid  
> > occurrence of a slash ('/'), I'm so used to java-thinking so I keep  
> > wanting to iterate through it and save the position of the third  
> > occurence and then cut the string at that index.
> > 
> > This doesnt seem to be the Ruby-way, anyone got ideas of how I should do  
> > it?

---

<div class="post-metadata">

**Author:** ![Lars\_Andren](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@Lars\_Andren](https://rubytalk.org/u/Lars_Andren)\
**Post date:** [28 June 2007 12:55 UTC](https://rubytalk.org/t/splitting-a-string/38697/4 "2007-06-28T12:55:26Z")

</div>

Thanks a lot both of you! I can use Stefanos method since I only want to  
use the part of the string before the third occurrence.  
Lars

Stefano Crocco wrote:

> **···**
>
> > Alle giovedì 28 giugno 2007, Lars Andren ha scritto:
> > 
> > > I've got a problem when trying to split a string after the thrid  
> > > occurrence of a slash ('/'), I'm so used to java-thinking so I keep  
> > > wanting to iterate through it and save the position of the third  
> > > occurence and then cut the string at that index.
> > > 
> > > This doesnt seem to be the Ruby-way, anyone got ideas of how I should do  
> > > it?  
> > > Grateful for any assistance,  
> > > Lars
> > 
> > There may be better solutions, but this should work:
> > 
> > str.scan(/(?:[^\/]+(?:\/|$)){3}/)
> > 
> > This returns an array of strings obtained by splitting str after every  
> > third /. The last entry of the array contains the rest of the string:
> > 
> > 'a/b/cd/efg/h/i'.scan(/(?:[^\/]+(?:\/|$)){3}/)  
> > =\> ["a/b/cd/", "efg/h/i"]
> > 
> > I hope this helps
> > 
> > Stefano
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [28 June 2007 18:09 UTC](https://rubytalk.org/t/splitting-a-string/38697/5 "2007-06-28T18:09:10Z")

</div>

In that case you can do

str.split("/")[0..2].join("/")

Cheers  
Robert

> **···**
>
> On 6/28/07, Lars Andren \<lars.andren@asia.chalmers.se\> wrote:
> 
> > Thanks a lot both of you! I can use Stefanos method since I only want to  
> > use the part of the string before the third occurrence.  
> > Lars
> 
> --  
> I always knew that one day Smalltalk would replace Java.  
> I just didn't know it would be called Ruby  
> -- Kent Back
