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
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:
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?
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: