Tip

Remove White Space with Regex

Here is a very handy extension method to remove white spaces from strings.

    1 public static string RemoveWhiteSpace(this string TextToRemoveWhiteSpaces)

    2 {

    3     string pattern = "\\s+";

    4     string replacement = " ";

    5     string result = Regex.Replace(TextToRemoveWhiteSpaces, pattern, replacement);

    6     return result;

    7 }

Adapted from http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx

Happy coding.

Category: Tip
Posts created 141

Leave a Reply

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top