I'm looking for a efficient way for removing all of the white spaces in an string. I have checked replace (replace(' ','')) but I'm looking for a more efficient way. I'd appreciate the help.
Asked
Active
Viewed 89 times
-3
-
2https://stackoverflow.com/questions/6219454/efficient-way-to-remove-all-whitespace-from-string – Nagaraj S Feb 05 '20 at 07:25
-
4Does this answer your question? [Efficient way to remove ALL whitespace from String?](https://stackoverflow.com/questions/6219454/efficient-way-to-remove-all-whitespace-from-string) – Ajit Panigrahi Feb 05 '20 at 07:29
-
Do you want to remove whitespace globally? then you can be used trim model binder from link https://stackoverflow.com/a/1734025/9334498 – Yasin Sunni Feb 05 '20 at 07:35
-
are you using EntityFramwork?? – Shakir Ahamed Feb 05 '20 at 07:40
-
Use the dupe Luke, it has many answers. Answers with benchmark of other answer. It doesn't blindly claim red car are faster. It provides tests cases and benchmark. – Drag and Drop Feb 05 '20 at 08:00
-
Yo have [here](https://stackoverflow.com/a/37347881/8024781) a list of ways of replacing whites, with a test class that measures the time it takes for each solution – ikerbera Feb 05 '20 at 08:58
1 Answers
0
You may use Regular Expression.
For example:
var result=System.Text.RegularExpressions.Regex.Replace(input, @"\s+", "");
Input is your string
See more Removing whitespaces using C#

Ramil Aliyev 007
- 4,437
- 2
- 31
- 47
-
1"must" is a strong word here. You _can_. Alternatively, you could also use something like `new string(input.Where(c => !char.IsWhiteSpace(c)).ToArray())`. Since Regex isn't magic, it _probably_ does something very similar to that. – Corak Feb 05 '20 at 07:38
-