# Splitting a string

**URL:** <https://www.ruby-forum.com/t/splitting-a-string/108019>\
**Category:** Ruby\
**Created:** [June 28, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/splitting-a-string/108019 "2007-06-28T13:51:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pappa-gottis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pappa-gottis](https://www.ruby-forum.com/u/pappa-gottis)\
**Post date:** [June 28, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/splitting-a-string/108019/1 "2007-06-28T13:51:43Z")

</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

---

<div class="post-metadata">

**Author:** ![pappa-gottis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pappa-gottis](https://www.ruby-forum.com/u/pappa-gottis)\
**Post date:** [June 28, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/splitting-a-string/108019/2 "2007-06-28T14:13:21Z")

</div>

Alle giovedÃ¬ 28 giugno 2007, Lars A. 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:** ![pappa-gottis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pappa-gottis](https://www.ruby-forum.com/u/pappa-gottis)\
**Post date:** [June 28, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/splitting-a-string/108019/3 "2007-06-28T14:55:20Z")

</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 C. wrote:

> Alle giovedÃ¬ 28 giugno 2007, Lars A. 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:** ![pappa-gottis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pappa-gottis](https://www.ruby-forum.com/u/pappa-gottis)\
**Post date:** [June 28, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/splitting-a-string/108019/4 "2007-06-28T14:26:22Z")

</div>

On 28.06.2007 13:51, Lars A. 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?

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

robert

---

<div class="post-metadata">

**Author:** ![pappa-gottis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pappa-gottis](https://www.ruby-forum.com/u/pappa-gottis)\
**Post date:** [June 28, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/splitting-a-string/108019/5 "2007-06-28T20:10:14Z")

</div>

On 6/28/07, Lars A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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  
> In that case you can do

str.split(“/”)[0…2].join(“/”)

Cheers  
Robert
