Solved

Remove non-alhanumericals from a string.

Posted on 2004-09-13
7
327 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

807 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