Skip to content

Instantly share code, notes, and snippets.

@pinalbhatt
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinalbhatt/19bfad106d5b8e17a595 to your computer and use it in GitHub Desktop.
Save pinalbhatt/19bfad106d5b8e17a595 to your computer and use it in GitHub Desktop.
/*
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment