Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove non-alhanumericals from a string.

Posted on 2004-09-13
7
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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