Closed Thread
Results 1 to 8 of 8

Thread: Remove Leading 0's

  1. #1
    NeedHelp Guest

    Remove Leading 0's

    I've been tasked with another "fix" which seems simple but isn't (to me). I need to remove all leading 0s of a string:

    "00000002035"

    needs to be

    " 2035"

    Is there some function I can use to do this already? The spacing needs to remain the same though.
    Last edited by NeedHelp; 03-29-2007 at 09:33 AM. Reason: Forgot to mention

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    you should be able to use regex

  4. #3
    NeedHelp Guest
    I found this:

    Perl Reference

    which lead me to making this:

    Code:
    $mystring =~ s/^0*//;
    $mystring =  sprintf("%- 18s", $mystring);
    And does what I need. Is this the best method though?

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    I don't know anything about perl so I cant tell you if thats the BEST way to do it. From my little understanding of regular expressions, it 'looks good.'

    My theory is, if it works leave it alone

  6. #5
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    Quote Originally Posted by NeedHelp View Post
    I found this:

    Perl Reference

    which lead me to making this:

    Code:
    $mystring =~ s/^0*//;
    $mystring =  sprintf("%- 18s", $mystring);
    And does what I need. Is this the best method though?
    Yes, that is a good way to do what you want.

  7. #6
    NeedHelp Guest
    Thanks for the input. This method seems to be working fine for now.

  8. #7
    ghostdog74 is offline Newbie
    Join Date
    Apr 2007
    Posts
    6
    Rep Power
    0
    some other ways:
    Code:
    $new = sprintf "%d" , "000023";
    print $new;
    Code:
    $val = "00032";
    $new = int $val; 
    print $new;

  9. #8
    NeedHelp Guest
    I never thought of the int $val method. That would work fine I would think.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How Do I remove?
    By byronwells in forum Database & Database Programming
    Replies: 2
    Last Post: 03-11-2010, 07:38 PM
  2. remove
    By zhenya in forum C and C++
    Replies: 1
    Last Post: 12-31-2009, 02:09 PM
  3. preg_replace Keep Leading 0's
    By rsnider19 in forum PHP Development
    Replies: 2
    Last Post: 11-05-2009, 08:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts