remove the first zero from a number

Posted on 2006-03-28
Last Modified: 2010-04-16
hi there ,

in the func i geting this kind of number 03178596
i want to dispose the zero but only if its the first digit in the num

how can i do it ?

thanks ...
Question by:Tech_Men
    LVL 21

    Accepted Solution

    You could convert it to a number and then back to a string again:

       string numResult="03178596";
       string trimedResult=long.parse(numResult).ToString();

    Or write a specialized trim function (probably faster)
    public class utilClass
     public static string trimLeading(string s, char trimChar)
        int startOffset=0;
        while(startOffset<s.Length-1) //If it is ALL 0's then leave the last one there
           if (s[startOffset]!=trimChar)
             break;   //Found 1st non-trimChar
          return s.SubString(startOffset);
    Then call it like this:
        string numResult="03178596";
        string trimedResult=utilClass.trimLeading(numResult, '0');
    LVL 2

    Expert Comment

                                   static void remove_zero(ref string num)
                            num = num.Remove(0,1);
    LVL 21

    Expert Comment

    rahul_r_jadhav's solution is not as efficient (though quite elegant).  Each time
      num = num.Remove(0,1);
    is called, a new string is created, which might slow your application down (a few seconds) with unnecessary garbage collections.
    LVL 52

    Expert Comment

    by:Carl Tawn
    Simply casting it to a numeric data type should drop any leading zeros:


            string num = "0123456789";
            long lnum = long.Parse(num);

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now