Solved

Remove non-alhanumericals from a string.

Posted on 2004-09-13
7
325 Views
Last Modified: 2010-04-15
Hi!

I have a string that I want to remove all letters from (A-Z and a-z). I want numbers (0-9) and other chars (struff like "-" and "/" to remain. Can somebody point me in the right direction on how to do this?

0
Comment
Question by:mjohansson
  • 4
  • 2
7 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 125 total points
ID: 12043919
I have test the code below,It's OK

//call part

string s="Aa12Ab-/45";
s=getNoCharString(s);

//method part

public string getNoCharString(string s);
{
     int i,j;

     s=s.ToLower();
     char []a=s.ToCharArray();
            
     j=0;
     for(i=0;i<a.Length;i++)
     {
         if((a[i]>='a') &&(a[i]<='z')) continue;
         a[j]=a[i];
         j++;
      }

      char []b=new char[j];
      Array.Copy(a,b,j);
      string s2=new string(b);
      return s2;
}
0
 
LVL 3

Expert Comment

by:a_goat
ID: 12044482
string MyString = @"This>< is .! a test() string, ok?";

string CleanString = System.Text.RegularExpressions.Regex.Replace(MyString, @"[^a-zA-Z0-9\.\\,\?]", "")

In the @"..." put the regex for all characters you want to keep.  That'll keep a-z, A-Z, 0-9, ".", ",", and "?".
0
 
LVL 3

Expert Comment

by:a_goat
ID: 12044514
There's a concise Regular Expression reference at http://www.visibone.com/regular-expressions/ but it can be tricky.  This one: http://www.crazygrrl.com/weav/reference.php3 might be a bit better
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:a_goat
ID: 12044561
If you want to do it the way the guy above did, you could at least do this

public string getNoCharString(string s);
{
     System.Text.StringBuilder foo = new System.Text.StringBuilder();
     
     for (char c in s)
          if (((a[i]>='a') &&(a[i]<='z')) || ((a[i]>='A') &&(a[i]<='Z')) || /* whatever else */)
               foo.Append(c)

     return foo.ToString()
}
0
 
LVL 3

Expert Comment

by:a_goat
ID: 12044571
Sorry, I copied and pasted too much, it's simpler:

public string getNoCharString(string s);
{
     System.Text.StringBuilder foo = new System.Text.StringBuilder();
     
     for (char c in s)
          if (((c>='a') &&(c<='z')) || ((c>='A') &&(c<='Z')) || /* whatever else */)
               foo.Append(c)

     return foo.ToString()
}
0
 

Author Comment

by:mjohansson
ID: 12045310
Thank you all. I accepted way too fast there, but you were all good. Thank you very much.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12045330
you're welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now