Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Created December 13, 2013 09:07
Show Gist options
  • Save sebnilsson/7941692 to your computer and use it in GitHub Desktop.
Save sebnilsson/7941692 to your computer and use it in GitHub Desktop.
Helper-functions to ensure string starts/ends with a specific string
public static class StringExtensions
{
public static string EnsureEndsWith(
this string value,
string endValue,
StringComparison comparisonType = StringComparison.CurrentCulture)
{
return (value == null || value.EndsWith(endValue, comparisonType)) ? value : (value + endValue);
}
public static string EnsureStartsWith(
this string value,
string startValue,
StringComparison comparisonType = StringComparison.CurrentCulture)
{
return (value == null || value.StartsWith(startValue, comparisonType)) ? value : (startValue + value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment