How do I remove all non alphanumeric characters from a string except dash and space characters?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I use a variation of one of the answers here. I want to replace spaces with "-" so its SEO friendly and also make lower case. Also not reference system.web from my services layer.
I´ve made a different solution, by eliminating the Control characters, which was my original problem.
It is better than putting in a list all the "special but good" chars
it´s simpler, so I think it´s better !
Replace
[^a-zA-Z0-9 -]
with an empty string.Here's an extension method using @ata answer as inspiration.
or if you require additional characters other than hyphen...
The regex is
[^\w\s\-]*
:\s
is better to use instead of space (), because there might be a tab in the text.
There is a much easier way with Regex.