Skip to content

Instantly share code, notes, and snippets.

@trailmax
Last active March 28, 2019 01:10
Show Gist options
  • Save trailmax/2e23ae499aefcc0077ec01cd8c234e8d to your computer and use it in GitHub Desktop.
Save trailmax/2e23ae499aefcc0077ec01cd8c234e8d to your computer and use it in GitHub Desktop.
String extensions
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