# Regular expressions - string substitution

**URL:** <https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955>\
**Category:** Ruby\
**Created:** [September 4, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955 "2009-09-04T16:21:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/1 "2009-09-04T16:21:59Z")

</div>

Hello all,

A quick question if I have a string like so

000000100

and I want to remove the leading 0’s to leave me with a string like so

100

What is the best way to do this? initially I was using  
string.delete(“0”) but that obviously deletes every 0 and is not the  
desired outcome. I then though string.gsub(/^0/, “”) however this only  
gets rid of the very first 0. Does anyone have any suggestions?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/2 "2009-09-04T16:28:20Z")

</div>

“000000100”.to\_i.to\_s # =\> 100

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/3 "2009-09-04T16:29:37Z")

</div>

So long as you’ll always be converting numbers, you can use to\_i. That  
will convert the string into an integer.

> “000000100”.to\_i  
> =\> 100

Keep in mind that it will truncate the number as soon as it reaches a  
non-number. For instance:

> “00012,345,678”.to\_i  
> =\> 12

If you need to keep decimals then you can use to\_f. For example:

> “0001234.567”.to\_f  
> =\> 1234.567

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/4 "2009-09-04T16:38:48Z")

</div>

Thanks all. I completely forgot about that. It always the simple things

Marc H. wrote:

> “000000100”.to\_i.to\_s # =\> 100

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/5 "2009-09-04T16:43:16Z")

</div>

On Fri, 4 Sep 2009 23:24:34 +0900  
Ne Scripter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> string.gsub(/^0/, “”)

Try something like this:

string.gsub(/^0\*/, “”)

This means: 0 or more occurrences of character “0”, starting at the  
beginning of the string.

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/6 "2009-09-04T16:42:45Z")

</div>

Ne Scripter wrote:

> Thanks all. I completely forgot about that. It always the simple things
> 
> Marc H. wrote:
> 
> > “000000100”.to\_i.to\_s # =\> 100

> > > I then though string.gsub(/^0/, “”) however this only  
> > > gets rid of the very first 0.

string.gsub(/^0+/,"")

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:45pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/7 "2009-09-04T16:45:02Z")

</div>

On Sep 4, 2009, at 9:24 AM, Ne Scripter wrote:

> What is the best way to do this? initially I was using  
> string.delete(“0”) but that obviously deletes every 0 and is not the  
> desired outcome. I then though string.gsub(/^0/, “”) however this only  
> gets rid of the very first 0. Does anyone have any suggestions?

“000000100”.sub(/\A0+/, “”)

\A means the beginning of the String and 0+ means one or more zeros.

Also note the use of sub() which only matches once over gsub() which  
is used to repeat for all matches. There can only be one run of zeros  
at the front of the String, so sub() is all we need.

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/8 "2009-09-04T16:47:58Z")

</div>

Hello,

> What is the best way to do this? initially I was using  
> string.delete(“0”) but that obviously deletes every 0 and is not the  
> desired outcome. I then though string.gsub(/^0/, “”) however this only  
> gets rid of the very first 0. Does anyone have any suggestions?

string.sub(/^0+/,’’)

should do what you are looking for.

Cheers,

---

<div class="post-metadata">

**Author:** ![nescripter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@nescripter](https://www.ruby-forum.com/u/nescripter)\
**Post date:** [September 4, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/regular-expressions-string-substitution/174955/9 "2009-09-04T16:53:14Z")

</div>

On Sep 4, 2009, at 10:24 AM, Ne Scripter wrote:

> What is the best way to do this? initially I was using  
> string.delete(“0”) but that obviously deletes every 0 and is not the  
> desired outcome. I then though string.gsub(/^0/, “”) however this only  
> gets rid of the very first 0. Does anyone have any suggestions?
> 
> ## Thanks a lot

string.sub(/^0\*/,‘’)

Or /^0+/ since both work if there are zeroes, but find nothing if the  
first character is not a 0.

If your intention is to convert to a number, then one of the other  
responses will work, but since you presented a string and asked to be  
left with a string, I wanted to be sure you saw this simple tweak to  
what you’d already discovered.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
