Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Created February 27, 2014 14:45
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 bobbychopra/9251444 to your computer and use it in GitHub Desktop.
Save bobbychopra/9251444 to your computer and use it in GitHub Desktop.
Helpful Extension Methods for Strings
namespace System
{
public static class StringExt
{
public static bool EqualsWithCaseIgnore(this string a, string b)
{
if (a == null && b == null)
return true;
else if (a == null && b != null)
return false;
else if (a != null && b == null)
return false;
else
return string.Equals(a.Trim(), b.Trim(), StringComparison.OrdinalIgnoreCase);
}
public static T As<T>(this string str)
{
var type = Nullable.GetUnderlyingType(typeof(T));
if (type == null)
type = typeof(T);
else
{
if (string.IsNullOrWhiteSpace(str))
return default(T);
}
return (T)Convert.ChangeType(str, type);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment