# Substring like in PHP's substr()?

**URL:** <https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456>\
**Category:** Ruby\
**Created:** [April 9, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456 "2007-04-09T13:52:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 9, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456/1 "2007-04-09T13:52:12Z")

</div>

Hi all

I’m looking for the coolest way to get the substring

/Webwork/pgbookings

out of

/Users/Josh/Webwork/pgbookings

The part /Users/Josh is saved in the variable my\_var.

In PHP I would do something like the following:

substr(/Users/Josh/Webwork/pgbookings, strlen(my\_var)) # =\>  
/Webwork/pgbookings

What’s the fastest way to do this in Ruby? 🙂  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 9, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456/2 "2007-04-09T14:04:32Z")

</div>

On Apr 9, 2007, at 6:52 AM, Joshua M. wrote:

> The part /Users/Josh is saved in the variable my\_var.
> 
> In PHP I would do something like the following:
> 
> substr(/Users/Josh/Webwork/pgbookings, strlen(my\_var)) # =\>  
> /Webwork/pgbookings
> 
> What’s the fastest way to do this in Ruby? 🙂

I’m not sure which way is faster, so let’s ask Ruby to time some of  
my ideas:

#!/usr/bin/env ruby -w

require “benchmark”

data = “/Users/Josh/Webwork/pgbookings”  
prefix = “/Webwork/pgbookings”

TESTS = 1\_000\_000  
Benchmark.bmbm do |results|  
results.report("[i…-i]:") { TESTS.times { data  
[prefix.length…-1] } }  
results.report("[i, l]:") { TESTS.times { data[prefix.length,  
data.length] } }  
results.report(“sub():”) { TESTS.times { data.sub(/\A#{prefix}/,  
“”) } }  
results.report("[re, c]:") { TESTS.times { data[/\A#{prefix}(.+)/,  
1] } }  
end

# \>\> Rehearsal --------------------------------------------

# \>\> [i…-i]: 2.500000 0.000000 2.500000 ( 2.511868)

# \>\> [i, l]: 0.630000 0.000000 0.630000 ( 0.634460)

# \>\> sub(): 6.800000 0.020000 6.820000 ( 6.850561)

# \>\> [re, c]: 7.180000 0.010000 7.190000 ( 7.204910)

# \>\> ---------------------------------- total: 17.140000sec

# \>\>

# \>\> user system total real

# \>\> [i…-i]: 2.500000 0.000000 2.500000 ( 2.500795)

# \>\> [i, l]: 0.630000 0.010000 0.640000 ( 0.633458)

# \>\> sub(): 6.840000 0.000000 6.840000 ( 6.853133)

# \>\> [re, c]: 7.110000 0.010000 7.120000 ( 7.130398)

**END**

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 9, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456/3 "2007-04-09T14:26:15Z")

</div>

James G. wrote:

> On Apr 9, 2007, at 6:52 AM, Joshua M. wrote:

Looks COOL… 😃

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 9, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456/4 "2007-04-09T16:10:05Z")

</div>

Thank you guys. 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 9, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456/5 "2007-04-09T15:33:50Z")

</div>

On 4/9/07, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> data = “/Users/Josh/Webwork/pgbookings”  
> prefix = “/Webwork/pgbookings”

I think you meant  
prefix = “/Users/Josh”

This doesn’t affect the qualitative results of the benchmark but it  
does change it to do what the OP was trying to do.

Note that “/Webwork/pgbookings” is NOT a prefix of  
“/Users/Josh/Webwork/pgbookings”

And cosmetically there’s a typo:

> results.report(“[i…-i]:”) { TESTS.times { data  
> [prefix.length…-1] } }

should be:

results.report(“[i…-1:”) { TESTS.times { data[prefix.length…-1] } }

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 9, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/substring-like-in-phps-substr/91456/6 "2007-04-09T16:07:58Z")

</div>

On Apr 9, 2007, at 8:33 AM, Rick DeNatale wrote:

> On 4/9/07, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > data = “/Users/Josh/Webwork/pgbookings”  
> > prefix = “/Webwork/pgbookings”
> 
> I think you meant  
> prefix = “/Users/Josh”
> 
> This doesn’t affect the qualitative results of the benchmark but it  
> does change it to do what the OP was trying to do.

Oops, I did. Thank you.

> And cosmetically there’s a typo:
> 
> > results.report(“[i…-i]:”) { TESTS.times { data  
> > [prefix.length…-1] } }
> 
> should be:
> 
> results.report(“[i…-1:”) { TESTS.times { data [prefix.length…-1] } }

Yeah, I blew that one, didn’t I?

Thanks for the corrections.

James Edward G. II
