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
You can try:
Where
s
is your string.Using System.Linq
I could have used RegEx, they can provide elegant solution but they can cause performane issues. Here is one solution
When using the compact framework (which doesn't have FindAll)
Replace FindAll with1
1 Comment by ShawnFeatherly
Want something quick?
This will allow you to specify which characters you want to allow as well.
Based on the answer for this question, I created a static class and added these. Thought it might be useful for some people.
Then the methods can be used as:
Here is a non-regex heap allocation friendly fast solution which was what I was looking for.
Unsafe edition.
And for those who don't want to use unsafe or don't trust the string length hack.