How to trim the 0

hi all,

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

@a=EDIA00050 (in this EDIA is common)
@[email protected](“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.

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)
@[email protected](“EDIA”)
render_text @t[1]

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

Fred

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)
@[email protected](“EDIA”)
render_text @t[1]

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

Fred

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)
@[email protected](“EDIA”)
render_text @t[1]

gsub:
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.
@[email protected](/^EDIA0*/, ‘’)
render_text @t[1]

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

hope it helps!
shai

Thank you so much shai, it works well

On 10/11/07, Shai R. [email protected] wrote:

gsub:
class String - RDoc Documentation

@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.
@[email protected](/^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/

Shai R. wrote:

@[email protected](/^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.

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

Thanks for your suggestion, i try to use meaningful words