Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace symbols from string in C#

Tags:

c#

regex

How can we replace symbols from string in C#?

Like this

Input : "�Click me."

Output: "Click me.";

like image 663
Novice Avatar asked Mar 02 '11 17:03

Novice


1 Answers

A simplistic solution would be to strip all non-ASCII characters from your string. There are a couple of ways to do this available on this question, the simplest of which would probably be:

string s = "�Click me.";
s = Regex.Replace(s, @"[^\u0000-\u007F]", "");

Although as mentioned, this may be an encoding/codepage issue -- using a regex here may not necessarily be the appropriate solution.

EDIT: Based on your comments, here are a couple other patterns you can try:

Remove all non-ASCII characters and ASCII control characters:

s = Regex.Replace(s, @"[^\u0020-\u007F]", "");

Remove everything except for alphanumeric ASCII characters:

s = Regex.Replace(s, @"[^A-Za-z0-9]", "");
like image 154
Donut Avatar answered Sep 30 '22 02:09

Donut