Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
C#

How to remove all non alphanumeric characters from a string in C#

- 28.09.23 - ErcanOPAK

Replace [^a-zA-Z0-9] with an empty string.

string str = "This-is-my+++***RegexPattern&Text";
Regex rgx = new Regex("[^a-zA-Z0-9]"); 
str = rgx.Replace(str, "");

OUTPUT: ThisismyRegexPatternText

If you want to add an exception then you should add this character like this to the regex pattern (let’s assume you wish to exclude the ampersand):

[^a-zA-Z0-9 &]

string str = "This-is-my+++***RegexPattern&Text";
Regex rgx = new Regex("[^a-zA-Z0-9 &]"); 
str = rgx.Replace(str, "");

OUTPUT: ThisismyRegexPattern&Text

 

Related posts:

How to get the Xth Day of the Week of the Year in C#

Important Tips To Write Clean Code In C# from Debendra Dash

IndexOf() and LastIndexOf() in C#

Write Cleaner Business Logic Using C# Switch Expressions

Post Views: 44

Post navigation

How to get the Xth Day of the Week of the Year in C#
How to insert results of a stored procedure into a temporary table

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Nov    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (915)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (772)
  • How to add default value for Entity Framework migrations for DateTime and Bool (772)
  • How to make theater mode the default for Youtube (575)
  • Add Constraint to SQL Table to ensure email contains @ (558)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (531)
  • Average of all values in a column that are not zero in SQL (505)
  • Find numbers with more than two decimal places in SQL (421)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (414)

Recent Posts

  • JavaScript Event Loop Misconceptions — Why setTimeout(fn, 0) Is NOT Instant
  • HTML5 Input Validation — Why Your Patterns “Don’t Work”
  • CSS Z-Index Hell — Why Elements Randomly Disappear (and the Real Fix)
  • .NET Core JSON Serializer Pitfalls — Why Your Properties Don’t Serialize
  • ASP.NET Core Middleware Order — Why Your App Breaks for No Reason
  • SQL The “Too Many Indexes” Problem — Why Over-Indexing Slows Everything
  • SQL Parameter Sniffing — The REAL Fix Nobody Explains Properly
  • C# HttpClient Misuse — The Root of Random Slowdowns / DNS Failures
  • C# Configuration Binding Hell — Why Your Options Return Null
  • C# Async “Hangs Forever” Problem — Why Task Never Completes Under Load

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (915)
  • Get the First and Last Word from a String or Sentence in SQL (798)
  • How to select distinct rows in a datatable in C# (772)
  • How to add default value for Entity Framework migrations for DateTime and Bool (772)
  • How to make theater mode the default for Youtube (575)

Recent Posts

  • JavaScript Event Loop Misconceptions — Why setTimeout(fn, 0) Is NOT Instant
  • HTML5 Input Validation — Why Your Patterns “Don’t Work”
  • CSS Z-Index Hell — Why Elements Randomly Disappear (and the Real Fix)
  • .NET Core JSON Serializer Pitfalls — Why Your Properties Don’t Serialize
  • ASP.NET Core Middleware Order — Why Your App Breaks for No Reason

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2025 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com