Delete non-alphanumeric characters, excluding space

I have this statement:

String cap = Regex.Replace(winCaption, @"[^\w\.@-]", ""); 

which converts "Hello | World!?"to "HelloWorld".

But I want to keep the space character, for example: "Hello | World!?"to "Hello  World".

How can i do this?

+3
source share
3 answers

just add a space to your character set, [^ \ w. @ -]

var winCaption = "Hello | World!?";
String cap = Regex.Replace(winCaption, @"[^\w\.@\- ]", "");

Please note that you need to avoid the "dash" (-) character, as it is usually used to indicate a range of characters (for example, [A-Za-z0-9])

+4
source

Here you go ...

string cap = Regex.Replace(winCaption, @"[^\w \.@-]", "");
+1
source

:

  String cap= Regex.Replace(winCaption, @"[^\w\.@\- ]", "");
0

Source: https://habr.com/ru/post/1748834/


All Articles