# Simple string operation

**URL:** <https://www.ruby-forum.com/t/simple-string-operation/108968>\
**Category:** Ruby\
**Created:** [July 10, 2007, 4:54am UTC](https://www.ruby-forum.com/t/simple-string-operation/108968 "2007-07-10T04:54:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 10, 2007, 4:54am UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/1 "2007-07-10T04:54:06Z")

</div>

seqNo =  
File.basename("/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV",".\*")[-1,3].to\_s  
seqNo = “-” + seqNo+ “.xx.m”  
puts "seqNo: #{seqNo} " # \>\> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

I didn’t even think the .to\_s should be necessary, but tried it in  
desperation.

Thanks

Newbie (but you already figured that out by the question).

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 10, 2007, 5:35am UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/2 "2007-07-10T05:35:28Z")

</div>

On 7/9/07, 12 34 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> seqNo =  
> File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.\*”)[-1,3].to\_s  
> seqNo = “-” + seqNo+ “.xx.m”  
> puts "seqNo: #{seqNo} " # \>\> seqNo: -4.xx.m
> 
> but I want it to be -004.xx.m. In other words I want the leading zeros.

Maybe you want [-3,3] instead of [-1,3]?

Todd

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 10, 2007, 5:53am UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/3 "2007-07-10T05:53:03Z")

</div>

Todd B. wrote:

> On 7/9/07, 12 34 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > seqNo =  
> > File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.\*”)[-1,3].to\_s  
> > seqNo = “-” + seqNo+ “.xx.m”  
> > puts "seqNo: #{seqNo} " # \>\> seqNo: -4.xx.m
> > 
> > but I want it to be -004.xx.m. In other words I want the leading zeros.
> 
> Maybe you want [-3,3] instead of [-1,3]?

Maybe I do.

Thank you

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 10, 2007, 6:03am UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/4 "2007-07-10T06:03:30Z")

</div>

On 10 Jul 2007, at 11:54, 12 34 wrote:

> desperation.
> 
> Thanks

Your use of String#[] is wrong:

-------------------------------------------------------------- String#[]  
Element Reference—If passed a single +Fixnum+, returns the code  
of the character at that position. If passed two +Fixnum+ objects,  
returns a substring starting at the offset given by the first, and  
a length given by the second.

So [-1,3] gives you a string starting at the last character that is 3  
characters long. You run off the end of the string before it gets to  
3 characters, so it ends up just being the last character - 4. I  
wonder if Ruby shouldn’t raise an IndexError in that case.

Replace it with [-4,4] to get what you want (I think) - the last 4  
characters. As you mention, the .to\_s is unnecessary.

seqNo = File.basename("/Volumes/MINOLTA1/DCIM/100MLT16/  
PICT0004.MOV",".\*")[-4,4]  
seqNo = “-#{seqNo}.xx.m”

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 10, 2007, 8:27am UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/5 "2007-07-10T08:27:39Z")

</div>

2007/7/10, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On 7/9/07, 12 34 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > seqNo =  
> > File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.\*”)[-1,3].to\_s  
> > seqNo = “-” + seqNo+ “.xx.m”  
> > puts "seqNo: #{seqNo} " # \>\> seqNo: -4.xx.m
> > 
> > but I want it to be -004.xx.m. In other words I want the leading zeros.
> 
> Maybe you want [-3,3] instead of [-1,3]?

Or

seqNo = File.basename(fname)[/\d+/]

cheers

robert

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 11, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/6 "2007-07-11T15:45:52Z")

</div>

On Jul 9, 8:54 pm, 12 34 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> seqNo =  
> File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.\*”)[-1,3].to \_s  
> seqNo = “-” + seqNo+ “.xx.m”  
> puts "seqNo: #{seqNo} " # \>\> seqNo: -4.xx.m
> 
> but I want it to be -004.xx.m. In other words I want the leading zeros.

Slim2:~ phrogz$ irb  
irb(main):001:0\> path = “/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”  
=\> “/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”  
irb(main):002:0\> digits = path[/(\d+).\w+$/, 1]  
=\> “0004”  
irb(main):003:0\> my\_seq = “%03d” % digits.to\_i  
=\> “004”

---

<div class="post-metadata">

**Author:** ![mtnbiker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@mtnbiker](https://www.ruby-forum.com/u/mtnbiker)\
**Post date:** [July 11, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/simple-string-operation/108968/7 "2007-07-11T15:44:51Z")

</div>

12 34 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> seqNo =  
> File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.\*”)[-1,3].to\_s  
> seqNo = “-” + seqNo+ “.xx.m”  
> puts "seqNo: #{seqNo} " # \>\> seqNo: -4.xx.m
> 
> but I want it to be -004.xx.m. In other words I want the leading zeros.

seqNo =  
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,"._")  
seqNo = “-” + /\d_$/.match(seqNo)[0] + “.xx.m”  
puts "seqNo: #{seqNo} "

But you might need to change that if you don’t want all three leading  
zeros (your desired answer omits one). m.
