Remove white spaces in a string c#

By using trim() we can remove white space characters from the beginning and end of a string.But to remove white space characters in middle we cannot use trim() method.

we can easily remove the white spaces/empty space in a string by replace method

string strSample = “Test with white space”;
strSample = strSample.Replace(” “, “”);

Result : strSample = “Testwithwhitespace”

2 thoughts on “Remove white spaces in a string c#

  1. string MyString = ” Big “;
    Console.WriteLine(“Hello{0}World!”, MyString);
    string TrimString = MyString.Trim();
    Console.WriteLine(“Hello{0}World!”, TrimString);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>