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.
amillyardAsked:
Who is Participating?
 
RedKelvinConnect With a Mentor Commented:
Hi, yeah that last code block is C# give it a go, it just requires a visualbasic reference to make use of isnumeric.

If you don't want to use that reference, then you can check for numerics using the examples in the last link I posted
0
 
RedKelvinCommented:
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/
0
 
RedKelvinCommented:
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
amillyardAuthor Commented:
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
0
 
RedKelvinCommented:
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;
        }
0
 
RedKelvinCommented:
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
0
 
amillyardAuthor Commented:
ok -- fyi, I am using c# (not vb)
0
 
amillyardAuthor Commented:
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?

0
 
amillyardAuthor Commented:
worked 1st time.

many thanks :-)
0
 
RedKelvinCommented:
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;
            }
        }
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.