# How to trim the 0

**URL:** <https://www.ruby-forum.com/t/how-to-trim-the-0/116934>\
**Category:** Rails\
**Created:** [October 11, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934 "2007-10-11T12:29:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/1 "2007-10-11T12:29:17Z")

</div>

hi all,

I want to remove o, which is present in the before statement, like

@a=EDIA00050 (in this EDIA is common)  
@t=@a.spilt(“EDIA”)  
render\_text @t[1]

so i will get the output like 000050, but i want 50 only, like this  
this record will be change 000150,001150 so i want to remove which is in  
front of the element ‘o’ it should not remove ‘0’, which is back, please  
let me know , how to do this.

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/2 "2007-10-11T12:40:35Z")

</div>

On 11 Oct 2007, at 11:29, Vidya V. wrote:

> hi all,
> 
> I want to remove o, which is present in the before statement, like
> 
> @a=EDIA00050 (in this EDIA is common)  
> @t=@a.spilt(“EDIA”)  
> render\_text @t[1]

a =~ /0\*([1-9]\d\*)/  
puts $1

Fred

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/3 "2007-10-11T12:47:00Z")

</div>

hi Fred, thanks for your reply, but i don’t know how to apply in my  
coding, can you tell me

Frederick C. wrote:

> On 11 Oct 2007, at 11:29, Vidya V. wrote:
> 
> > hi all,
> > 
> > I want to remove o, which is present in the before statement, like
> > 
> > @a=EDIA00050 (in this EDIA is common)  
> > @t=@a.spilt(“EDIA”)  
> > render\_text @t[1]
> 
> a =~ /0\*([1-9]\d\*)/  
> puts $1
> 
> Fred

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/4 "2007-10-11T13:14:50Z")

</div>

Vidya V. wrote:

> hi all,
> 
> I want to remove o, which is present in the before statement, like
> 
> @a=EDIA00050 (in this EDIA is common)  
> @t=@a.spilt(“EDIA”)  
> render\_text @t[1]

gsub:  
[http://www.ruby-doc.org/core/classes/String.html#M000832](http://www.ruby-doc.org/core/classes/String.html#M000832)

@a=EDIA00050 (in this EDIA is common)

# this will work given that the string always starts with EDIA and

follows immediately with 0 or more zero’s.  
@t=@a.gsub(/^EDIA0\*/, ‘’)  
render\_text @t[1]

i’d advise looking into regexp a little deeper if this seems odd to you.

> **[Regular expression | Syntax](https://en.wikipedia.org/wiki/Regular_expression#Syntax)**
>
> A regex pattern matches a target string. The pattern is composed of a sequence of atoms. An atom is a single point within the regex pattern which it tries to match to the target string. The simplest atom is a literal, but grouping parts of the pattern to match an atom will require using ( ) as metacharacters. Metacharacters help form: atoms; quantifiers telling how many atoms (and whether it is a greedy quantifier or not); a logical OR character, which offers a set of alternatives, and a logical ...

hope it helps!  
shai

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/5 "2007-10-11T13:21:33Z")

</div>

Thank you so much shai, it works well

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/6 "2007-10-11T14:25:50Z")

</div>

On 10/11/07, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> gsub:  
> [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000832)
> 
> @a=EDIA00050 (in this EDIA is common)
> 
> # this will work given that the string always starts with EDIA and
> 
> follows immediately with 0 or more zero’s.  
> @t=@a.gsub(/^EDIA0\*/, ‘’)  
> render\_text @t[1]

And if you don’t need to save the value in an instance variable just:

```
render_text @a.gsub(/^EDIA0*/, '')

```

I’ve got no idea what @a and @t represent, or whether they should  
really BE instance variables or locals.

One strong suggestion to the OP is to use meaningful variable names.

–  
Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/7 "2007-10-11T15:56:34Z")

</div>

Shai R. wrote:

> @t=@a.gsub(/^EDIA0\*/, ‘’)

gsub does the job, but match is designed to return parts of a string  
that “match”:

EDIA\_ID = /[1-9]\d\*$/  
@t = @a.match(EDIA\_ID)[0]  
=\>“50”

…and can be used to get multiple matches at a time, which is sometimes  
useful (though probably not in this case):

EDIA\_PARTS = /^(._)([1-9]\d_)$/  
all, prefix, edia\_id = a.match(EDIA\_PARTS).captures  
=\> [“EDIA00050”, “EDIA000”, “50”]  
edia\_id  
=\> “50”

match returns a MatchData object whose “matches” can be accessed with  
the [] method – like an array (though you have to use captures or to\_a  
to actually get an array). [0] returns the entire data matched, and [1]+  
return the parenthetical submatches (like a regular expression replace  
string with \1 or $1 backreferenced subpatterns). The MatchData object  
also retains information about where the matches were found in the  
original string which can be accessed later with various methods.

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/8 "2007-10-11T16:46:16Z")

</div>

hi vidya,

I think, simply u can also use like below for this scenario:

“@t[1].to\_i”

this will return the integer value right i.e 50

Regards,

P.Gokula murthy

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/how-to-trim-the-0/116934/9 "2007-10-11T14:34:07Z")

</div>

Thanks for your suggestion, i try to use meaningful words
