One-Line C# #1 : Remove Whitespace from a string

Thought I'd post a quick extension method that I made today. Hope it helps someone.

 

public static string RemoveWhitespace(this string str)
{
    try
    {
        return new Regex(@"\s*").Replace(str, string.Empty);
    }
    catch (Exception)
    {
        return str;
    }
}
Published Friday, June 27, 2008 2:37 PM by zowens
Filed under: , , ,

Comments

# re: One-Line C# #1 : Remove Whitespace from a string

Friday, June 27, 2008 3:47 PM by Josh Schwartzberg

So it very rarely a good idea to suppress exceptions, this leads to problems in your code down the road that are hard to track down.

You could also avoid Regex and make the code simpler by doing the following:

public static string RemoveWhitespace(this string str)

{

       return (str ?? string.Empty).Replace(" ", string.Empty);

}

# re: One-Line C# #1 : Remove Whitespace from a string

Friday, June 27, 2008 5:12 PM by zowens

Not sure what you mean by express expectations. Plus your code doesn't remove the whitespace form the string, it just replaces "" with string.empty, the same value.

# re: One-Line C# #1 : Remove Whitespace from a string

Friday, June 27, 2008 5:13 PM by AndrewSeven

I'd be more explicit and do nothing if the string were null or empty.

I think the regEx is to remove tabs and other whitespace, not just a space.

if(String.IsNullOrEmpty(str))

{

return str;

}

else

{

return ...

}

# One-Line C# #1 : Remove Whitespace from a string | MT-Soft Website Development

Pingback from  One-Line C# #1 : Remove Whitespace from a string | MT-Soft Website Development

# re: One-Line C# #1 : Remove Whitespace from a string

Saturday, June 28, 2008 12:27 AM by zowens

Ah should have checked for empty and null. Good point.

# re: One-Line C# #1 : Remove Whitespace from a string

Saturday, June 28, 2008 4:52 AM by Uwe

"catch (Exception)" is _always_ a bad idea. Only catch exceptions if you handle them. Otherwise don't!

# re: One-Line C# #1 : Remove Whitespace from a string

Saturday, June 28, 2008 7:37 AM by kamii47

ANy idea of a rugular expression to convert multiple spaces to single one.[also]

like " k  amr a   n " into "k amr a n"

# re: One-Line C# #1 : Remove Whitespace from a string

Saturday, June 28, 2008 10:58 AM by zowens

@Uwe But sometimes you just don't care and just want the application to keep moving with out something stupid (like removing whitespace) to slow you down.

# re: One-Line C# #1 : Remove Whitespace from a string

Saturday, June 28, 2008 10:59 AM by zowens

@kamii47 Not sure, I'll do some research. Not a big regex builder guy.. trying to get into it though!

# re: One-Line C# #1 : Remove Whitespace from a string

Monday, June 30, 2008 2:08 PM by Richard

@kamii47:

Try Regex.Replace(value, @"\s{2,}", " ")

# re: One-Line C# #1 : Remove Whitespace from a string

Monday, July 14, 2008 6:57 AM by Graham

If you *know* an exception can be raised then do what you can to prevent it in code *before* the exception is raised. Raising an exception is heavyweight C# in that it makes the CLR do a lot of work. You don't want to do this as part of a normal sequence of events. Then, let the application exception handler deal with the things that were really unexpected.

Leave a Comment

(required) 
(required) 
(optional)
(required)