?
Solved

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

Posted on 2007-10-11
10
Medium Priority
?
1,088 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:amillyard
  • 6
  • 4
10 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20062517
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20062523
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
 

Author Comment

by:amillyard
ID: 20062535
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:RedKelvin
ID: 20062567
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
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20062569
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
 

Author Comment

by:amillyard
ID: 20062579
ok -- fyi, I am using c# (not vb)
0
 
LVL 22

Accepted Solution

by:
RedKelvin earned 2000 total points
ID: 20062586
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
 

Author Comment

by:amillyard
ID: 20062609
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
 

Author Comment

by:amillyard
ID: 20062621
worked 1st time.

many thanks :-)
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20062625
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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question