Sachin Kainth
Sachin Kainth

Reputation: 46740

Replace all non-word characters with a space

I have a regex

[A-Za-z]

and a string, such as

Hi! This is a string.

I want to replace all charcters that are not in the Regex with space. So, I'll end up with

Hi This is a string

How is this done?

Upvotes: 7

Views: 2322

Answers (2)

Austin Salonen
Austin Salonen

Reputation: 50215

var cleaned = Regex.Replace(given, "[^A-Za-z]", " ");

Upvotes: 14

Richard Deeming
Richard Deeming

Reputation: 31198

Try:

string output = Regex.Replace(input, "[^A-Za-z]", " ");

Upvotes: 5

Related Questions