Last active
March 28, 2019 01:10
-
-
Save trailmax/2e23ae499aefcc0077ec01cd8c234e8d to your computer and use it in GitHub Desktop.
String extensions
This file contains 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 StringExtensions | |
{ | |
public static bool Contains(this string source, string toCheck, StringComparison comp) | |
{ | |
return source?.IndexOf(toCheck, comp) >= 0; | |
} | |
public static bool IsNullOrEmpty(this String @string) | |
{ | |
return String.IsNullOrEmpty(@string); | |
} | |
public static bool IsNullOrWhiteSpace(this String @string) | |
{ | |
return String.IsNullOrWhiteSpace(@string); | |
} | |
public static string SafeSubstring(this string text, int start, int length) | |
{ | |
if (text == null) | |
{ | |
return null; | |
} | |
return text.Length <= start ? "" | |
: text.Length - start <= length ? text.Substring(start) | |
: text.Substring(start, length); | |
} | |
public static bool SafeContains(this string text, string searchTerm) | |
{ | |
if (text.IsNullOrEmpty()) | |
{ | |
return false; | |
} | |
return text.Contains(searchTerm); | |
} | |
public static bool SafeStartsWith(this string text, string searchTerm) | |
{ | |
if (text.IsNullOrEmpty()) | |
{ | |
return false; | |
} | |
return text.StartsWith(searchTerm); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment