Solved

Remove non-alhanumericals from a string.

Posted on 2004-09-13
7
324 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:a_goat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
you're welcome
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Debug Windows Service program in C# 4 31
Aspnet xml 2 36
Duplicate a row 2 29
Round up to 100% in .NET 10 42
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

15 Experts available now in Live!

Get 1:1 Help Now