HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How do I remove all non alphanumeric characters from a string except dash?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
alphanumerichowfromremovedashnonexceptcharactersstringall

Problem

How do I remove all non alphanumeric characters from a string except dash and space characters?

Solution

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

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");

Code Snippets

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");

Context

Stack Overflow Q#3210393, score: 1113

Revisions (0)

No revisions yet.