pinalbhatt
8/3/2014 - 1:42 PM

StringExtensionMethods.cs

/*
C# Some Handy String Extension Methods
Code Snippet By: Pinal Bhatt [www.PBDesk.com]
*/

using System;

public static class StringExtensions
{
    	/// <summary>
        /// Indicates whether invoking string object is null or an empty string.
        /// </summary>
        /// <param name="inputstr"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string inputstr)
        {
            return string.IsNullOrEmpty(inputstr);
        }
    
     	/// <summary>
        /// Returns true if invoking string matches with regex pattern
        /// </summary>
        /// <param name="original"></param>
        /// <param name="regex"></param>
        /// <returns></returns>
        public static bool IsRegexMatch(this string original, string regex)
        {
            return Regex.IsMatch(original, regex);
        }
        
        /// <summary>
        /// Indicates whether invoking string object is not null and not an empty string.
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static bool IsNotNullAndNotEmpty(this string inputString)
        {
            return !string.IsNullOrEmpty(inputString);
        }
        
        /// <summary>
        /// Returns an empty string if input string is null.
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string IfNullThenEmpty(this string inputstr)
        {
            return inputString ?? string.Empty;
        }
}