Solved

Remove non-alhanumericals from a string.

Posted on 2004-09-13
7
328 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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 antispam), the admini…

733 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