Created
February 27, 2014 14:45
-
-
Save bobbychopra/9251444 to your computer and use it in GitHub Desktop.
Helpful Extension Methods for Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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