Click here to Skip to main content
15,393,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all.
i want to remove all special character or all other character but i do not want to remove like @ / : = etc and alpha bates.
what is the best way to do it
string mystring="♫♠~.┘&╤+@\????v?*=?\n?E???M???u??????r[?????o?W,~ ?:";
Posted
Updated 9-Sep-14 5:36am
v2
Comments
Mike Hankey 9-Sep-14 11:35am
   
Try Regex.Replace it can be used to replace any special characters see http://www.regular-expressions.info/replacetutorial.html and if you want a good tool for testing Regex expressions try Expresso!
Jawad Ahmed Tanoli 9-Sep-14 11:39am
   
Regex.Replace(System.Text.Encoding.ASCII.GetString(mystring, @"[^0-9a-zA-Z]+", ".");
i am using this but it will replace all characters i want that some character will not be removed
Andreas Gieriet 9-Sep-14 13:35pm
   
How about leaving away the ASCII thingy? Why do you convert to ASCII **before** replacing?
Andi
jaket-cp 16-Sep-14 9:59am
   
you should be able to add @ / : = into your regex pattern
e.g.: [^0-9a-zA-Z@/:=]+
Try that let us know how it works out
BillWoodruff 16-Sep-14 12:12pm
   
You need to define exactly the set of "special characters" you want to remove.

C#
string mystring="♫♠~.┘&╤+@\????v?*=?\n?E???M???u??????r[?????o?W,~ ?:";
string test="";
char[] allowCharacter = new char[] {' ','@','\n','\r','.',':','[',']','/','*',';','=','-','"','?','+','$','!','~','#','^','&','(',')','_','-','>','<','!','`', '{','}','|'};
              foreach (char item in mystring)
              {
                  if (allowCharacter.Contains(item) || char.IsLetter(item) || char.IsNumber(item))
                  {
                      test = test + item.ToString();

                  }
                 
                  else
                  {
 test = test + ".";
                  }
              }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900