How to remove all whitespace in C#

There a multiple way to remove whitespace in a string in C#. You can use the simple Trim(), TrimStart(), TrimEnd(). But to remove all whitespace you can’t only use the trim function as that is only remove whitespace on start and end.

The most efficient way to remove all whitespace is using the replace function

body.Replace(" ", "");

This will remove all trailing and in between white spaces in the string.