Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Handy Extension Methods

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 Jun 2011CPOL 4.3K  
Here is a couple for String and StringBuilder. The String versions are handy (compared to the already existing instance versions (that are actually being used)) because they work against null values (e.g., ((string)null).IsNullOrEmpty()).public static class ExtensionMethodsString{ ...

Here is a couple for String and StringBuilder. The String versions are handy (compared to the already existing instance versions (that are actually being used)) because they work against null values (e.g., ((string)null).IsNullOrEmpty()).


C#
public static class ExtensionMethodsString
{
    public static bool IsNullOrWhiteSpace(this string psString)
    {
        return string.IsNullOrWhiteSpace(psString);
    }
    public static bool IsNullOrEmpty(this string psString)
    {
        return string.IsNullOrEmpty(psString);
    }
}

C#
public static class ExtensionMethodsStringBuilder
{
    public static bool IsNullOrWhiteSpace(this StringBuilder poStringBuilder)
    {
        if (poStringBuilder.IsNullOrEmpty() == false)
            for (int x = poStringBuilder.Length - 1; x >= 0; x--)
                if (char.IsWhiteSpace(poStringBuilder[x]) == false)
                    return false;
        return true;
    }
    public static bool IsNullOrEmpty(this StringBuilder poStringBuilder)
    {
        return (poStringBuilder == null) || (poStringBuilder.Length == 0);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
FDW
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --