Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
I have string with special characters.so how to
replace special characters in c# like "<[^>]+/\'.{}()#$*@!:;?>."
or in sql at once.
Posted
Updated 17-Feb-22 1:10am

C#
using System.Text.RegularExpressions;
public string RemoveSpecialCharacters(string str)
{
    return Regex.Replace(str, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
}


It is tested Ok. Hope you find your solution.
 
Share this answer
 
v2
try:
C#
string dirty = "Dirty sql_ > uni^#@ unicode!";
string clean = Regex.Replace(dirty, @"([^a-zA-Z0-9_]|^\s)", string.Empty);
 
Share this answer
 
v2
You just need to loop through the string and replace the values. Try using this function:
C#
public static string fnRemoveSplChars(string strMyString) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in strMyString) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') | || (c >= 'a' && c <= 'z') ) {
         sb.Append(c);
      }
   }
   return sb.ToString();
}



--Amit
 
Share this answer
 
v2
Comments
prabu19 12-Mar-14 0:40am    
what is the use of => this symbol in c# .net

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