Link to home
Start Free TrialLog in
Avatar of amillyard
amillyardFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c#, asp parse string/text entry -- for any non-numeric characters and strip them from that string

Development Platform : c#, asp.net 2.x, Visual Studio Pro utilising Web Developer, IIS 6, SQL 2005 server

Have an asp form with a text entry box setup.

Want to be able to parse that string/text entry -- for any non-numeric characters and strip them from that string.

How is this done with c# / asp.net  please?

Your time and efforts with this enquiry are much apprieated.
Avatar of RedKelvin
RedKelvin

here is a vb.net example, is only short, would not take much to convert
http://www.aspfree.com/c/a/ASP-Code/Strip-NonNumeric-characters-from-string-function-by-Chad-Scarborough/
converted it, here you go

     public object StripNonNumeric(object strInput)
     {
         object iPos;
         object sNew;
         object iTemp;
         strInput = Strings.Trim(strInput);
         if (strInput != "") {
             iPos = 1;
             iTemp = Strings.Len(strInput);
             while (iTemp >= iPos) {
                 if (Information.IsNumeric(Strings.Mid(strInput, iPos, 1)) == true) {
                     sNew = sNew + Strings.Mid(strInput, iPos, 1);
                 }
                 iPos = iPos + 1;
             }
         }
     }

and of course you could always use regex, if you dont like that method
http://blogs.msdn.com/ericgu/archive/2005/11/21/495383.aspx
Avatar of amillyard

ASKER

thanks for that - apprieated.

now...just cutting and pasting into the asp c# f.cs file -- I am getting the following compile message:

The name 'Strings' does not exist in the current context
Terribly sorry, I converted it without checking.

This works, you will need to add a reference to Microsoft.VisualBasic

        public string StripNonNumeric(string strInput)
        {
            int iPos;
            string sNew = string.Empty;
            int iTemp;
            strInput = strInput.Trim();
            if (strInput != "")
            {
                iPos = 0;
                iTemp = strInput.Length-1;
                while (iTemp >= iPos)
                {
                    if (Microsoft.VisualBasic.Information.IsNumeric(strInput.Substring(iPos, 1)) == true)
                    {
                        sNew = sNew + strInput.Substring(iPos, 1);
                    }
                    iPos = iPos + 1;
                }
            }

            return sNew;
        }
ah, and if you don't want to make the visualbasic reference, here are some other ways to check for numerics
http://www.codeproject.com/useritems/csharp-isnumeric.asp

RedK
ok -- fyi, I am using c# (not vb)
ASKER CERTIFIED SOLUTION
Avatar of RedKelvin
RedKelvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok -- have added a vb reference.

does this have any affect elsewhere on programming and performance in terms of the asp site...i.e. using c# and vb code within the same page / site etc?

worked 1st time.

many thanks :-)
no there is no impact, but as I say, if you do not like that reference, you can remove it and write your own isnumeric function like this

        public bool IsNumeric(string str)
        {
            try
            {
                int result = int.Parse(str);
                return true;
            }
            catch
            {
                return false;
            }
        }