orilon

C# Extension Method - Check if string is (not) null/empty

Jun 4th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. /// <summary>
  2. /// Checks if the string is NULL or empty, then returns true, otherwise returns false.
  3. /// Example:
  4. ///     if (someTextBox.Text.IsNullOrEmpty())
  5. ///     {
  6. ///         // handle as true (empty)...
  7. ///     }
  8. ///     else
  9. ///     {
  10. ///         // handle as false (not empty)...
  11. ///     }
  12. /// </summary>
  13. /// <param name="inString"></param>
  14. /// <returns></returns>
  15. public static bool IsNullOrEmpty(this string inString)
  16. {
  17.     if (inString == null)
  18.         return true;
  19.     else if (inString.Trim() == string.Empty)
  20.         return true;
  21.     else
  22.         return false;
  23. }
  24.  
  25. /// <summary>
  26. /// Determines whether string is not null or empty.
  27. /// </summary>
  28. /// <param name="input">The input.</param>
  29. /// <returns>
  30. ///   <c>true</c> if [is not null or empty] [the specified input]; otherwise, <c>false</c>.
  31. /// </returns>
  32. public static bool IsNotNullOrEmpty(this string input)
  33. {
  34.     return !String.IsNullOrEmpty(input);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment