splitting a string

L

Lars Andren

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
 
S

Stefano Crocco

Alle gioved=C3=AC 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=20
third /. The last entry of the array contains the rest of the string:

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

I hope this helps

Stefano
 
R

Robert Klemme

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
 
L

Lars Andren

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 said:
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
 
R

Robert Dober

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top