remove the first zero from a number

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 ...
Tech_MenAsked:
Who is Participating?
 
MogalManicCommented:
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
       startOffset++;
    }
      return s.SubString(startOffset);
 }
}
Then call it like this:
    string numResult="03178596";
    string trimedResult=utilClass.trimLeading(numResult, '0');
0
 
rahul_r_jadhavCommented:
                               static void remove_zero(ref string num)
            {
                  while(num.StartsWith("0"))
                        num = num.Remove(0,1);
                  
            }
0
 
MogalManicCommented:
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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Simply casting it to a numeric data type should drop any leading zeros:

i.e.

        string num = "0123456789";
        long lnum = long.Parse(num);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.