trimming leading zeros

Discussion in 'Ruby' started by Dipesh Batheja, Dec 29, 2007.

  1. Is there any function in ruby which allows you to trim leading zeros (or
    any specific character) from a string. Or if there is some reg
    expression that i can use?
    --
    Posted via http://www.ruby-forum.com/.
     
    Dipesh Batheja, Dec 29, 2007
    #1
    1. Advertising

  2. Dipesh Batheja

    Young Moon Guest

    On Dec 29, 5:00 pm, Dipesh Batheja <> wrote:
    > Is there any function in ruby which allows you to trim leading zeros (or
    > any specific character) from a string. Or if there is some reg
    > expression that i can use?
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Hi,

    for simple character replacement, you may do something like
    "####helloworld".gsub!(/^#+/,'')
    the same can be written in String class if you use this too often,
    class String
    def trim_lead(n)
    self.gsub!(/^#{n}+/,'')
    end
    end
    puts "00001234".trim_lead("0")

    but when you use this to replace any special characters used
    internally by regex (for e.g., $)
    you have to do something like "$$$1234".trim_lead("\\$")
    Others may suggest better ways.
     
    Young Moon, Dec 29, 2007
    #2
    1. Advertising

  3. Dipesh Batheja wrote:

    > Is there any function in ruby which allows you to trim leading zeros (or
    > any specific character) from a string. Or if there is some reg
    > expression that i can use?


    Hi
    if you have string expressions like

    s = 0{1,*}[1-9]* (some number with leading zeros)

    perhaps the must obvious method is

    s.to_i.to_s

    :)

    but this isn't genralizable to other kind of leading characters,
    for them a regexp may be better

    Tom
     
    Tomaso Tosolini, Dec 29, 2007
    #3
  4. On 29.12.2007 13:00, Dipesh Batheja wrote:
    > Is there any function in ruby which allows you to trim leading zeros (or
    > any specific character) from a string. Or if there is some reg
    > expression that i can use?


    The simplest and fastest is probably

    s.sub! /\A0+/, ''

    Kind regards

    robert
     
    Robert Klemme, Dec 29, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,110
    Oliver Sturm
    Oct 13, 2005
  2. Luke Zhang [MSFT]

    Re: CSV for Excel - Problem with Leading Zeros

    Luke Zhang [MSFT], Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,262
    Luke Zhang [MSFT]
    Jun 26, 2003
  3. GIMME
    Replies:
    5
    Views:
    43,830
    Dale King
    Feb 13, 2004
  4. rayw

    trimming leading zeros

    rayw, Dec 14, 2005, in forum: C Programming
    Replies:
    8
    Views:
    563
  5. Generic Usenet Account

    Trimming leading and trailing spaces from a string

    Generic Usenet Account, Jul 8, 2005, in forum: C++
    Replies:
    6
    Views:
    396
    Paul Groke
    Jul 13, 2005
Loading...

Share This Page