• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

asp.net remove leading zeros from user input

I have a job number text box that requires three numerics (999).  In the code behind I want to store a string value of the number with no leading zeros.  IE, job '009' should store '9'.  job '099' should store '99', etc...

Does .net have a function for that?
0
JT_SIRO
Asked:
JT_SIRO
1 Solution
 
Scott CraigWebmasterCommented:
You should be able to use TrimStart:

string a = "000012340";
string b = a.TrimStart('0');

Open in new window

0
 
NavneetSoftware EngineerCommented:
Hi!

Convert.ToInt32("009");

Thanks!
0
 
käµfm³d 👽Commented:
Or you might prefer to validate that you have a number also:

int x;

if (int.TryParse(textBox1.Text, out x))
{
    // text is a valid number
    string y = x.ToString();
}

Open in new window

0
 
JT_SIROAuthor Commented:
oooh, TrimStart!  I didn't know about that.  I wouldn't have had to written this:

public string trimZeros(string s)    
    {
        int len;
        len = s.Length;

        s = s.Trim();

        if (s.Substring(0, 1) == "0")
        {
            s = s.Substring(1, len - 1);
            if (s.Substring(0, 1) == "0")
            {
                s = s.Substring(1, 1);
            }
        }

        return s;
    }
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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