Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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 ...
0
Tech_Men
Asked:
Tech_Men
  • 2
1 Solution
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now