09 Feb 2010

Tip: New .NET 4 String Function to Check for Empty Strings

     

How many times have you written code similar to this?

string firstName = FirstName.Text.Trim();

if (!string.IsNullOrEmpty(firstName))
{
    // do something
}

or

if (someParam == null || string.IsNullOrEmpty(someParam.Trim())
    throw new ArgumentException("Empty string", "someParam");

// do something

A nice little time-saver for those guard functions is now included in .NET 4: the IsNullOrWhiteSpace function:

if (!string.IsNullOrWhiteSpace(FirstName.Text))
{
    // do something
}

IsNullOrWhiteSpace checks for null, empty or whitespace characters.

Sure, it’s a little thing, but it’s nice to have it in there, especially considering we get all sorts of nice new little things, and the client framework install is still smaller than it was in .NET 3.5

Share |
posted by Pete Brown on Tuesday, February 09, 2010
filed under:      

15 comments for “Tip: New .NET 4 String Function to Check for Empty Strings”

  1. Joesays:
    Does it avoid the allocation that Trim would give you? (i.e. perform better)
  2. jschroedlsays:
    ooh, nice - thanks for the tip
  3. TAGsays:
    TrimStart() can avoid allocation.

    There is no reasons to do trim from both sides - as if string is empty - then it will be empty even then trimming from start.

    But! This practice is flawed. In case if you are trimming strings for purpose of validation - then you should deal with trimmed string for actual processing.

    So you should check for null or empty, then trim and save, then check again, then process data using trimmed data.
  4. Jadawinsays:
    Time for a new extension method! I guess it'll replace my .IsNullOrTrimIsEmpty() one. :)
  5. Oskar Austegardsays:
    nice, but why aren't IsNullOrEmpty and IsNullOrWhiteSpace extension methods?

    What reads better?

    if (!string.IsNullOrWhiteSpace(FirstName.Text))

    or

    if (!FirstName.Text.IsNullOrWhiteSpace())
  6. Pete Brownsays:
    @Oskar

    But what if it is null?

    Pete
  7. Bob Valesays:
    <p>But an extension method doesn't care if it is null because the compiler rewrites it from </p><p>FirstName.Text.IsNullOrWhiteSpace() to string.IsNullOrWhiteSpace(FirstName.Text) you can just use the easier to read form.</p>
  8. Jacob O'Reillysays:
    @Pete.Brown
    Since it would be an extension method, it wouldn't be a problem that the string being used was null. Under the covers it just calls the method with the string as its parameter.
  9. Denissays:
    I've tested extension methods on null strings. Not a problem as long as you test the 'this string' parameter for null.
  10. Khalossays:
    @Pete.Brown
    If they are were extension methods like Oskar suggests, it doesn't matter if the instance is null. Extension methods are just syntactic sugar.

    s.ExtensionMethod() is the same as ExtensionClass.ExtensionMethod(s) even is s is null.

    That's one of the great uses of extension methods, which I think is what Oskar was getting it, they didn't use extension methods, so we can't do this.
  11. Erik van Brakelsays:
    @Pete.Brown: It would still work, but I would agree if you said it would be odd. This would work:

    public static bool IsNullOrWhiteSpace(this input)
    {
    return string.IsNullOrWhiteSpace(input);
    }

    As long as you're not calling any methods/properties on the input string it won't fail if it's null. Looks odd though.
  12. Pete Brownsays:
    True all.

    I should have stated that it looks really wrong to me when the value is null. I'll get over it, though :)

    As for extension methods in general, as pointed out, you can do that yourself if you want. I think the problem with extension methods in the framework is that it isn't always discoverable. Adding in a namespace (if the method is in System no problems) but adding in a namespace and suddenly getting new functionality on existing types tends to screw new folks up. Linq is a good example of this.

    I like extension methods for their utility, but I think they have discoverability issues.

    Pete
  13. Josh Einsteinsays:
    I hate the idea of it being an extension method because as noted, it feels totally wrong to have some methods (extension methods) work on null references and others not.

    To Joe up top, yes it avoids the allocation by checking each character with Char.IsWhiteSpace. It doesn't do Trim().Length internally.
  14. Richardsays:
    <p>Of course, it's trivial to write your own version of this function in v3.5: </p><p>public static bool IsNullOrWhiteSpace(this string value) </p><p>{ </p><p>  if (string.IsNullOrEmpty(value)) </p><p>    return true; </p><p>  return value.All(char.IsWhiteSpace); </p><p>} </p>

Comment on this Post

Remember me

1 trackback for “Tip: New .NET 4 String Function to Check for Empty Strings”

  1. uberVU - social commentssays:
    This post was mentioned on Twitter by ash2mud: It's the little things, new in .NET 4: string.IsNullOrWhiteSpace() http://bit.ly/b5Bhp1