Skip to content

Instantly share code, notes, and snippets.

@derantell
Created February 18, 2013 15:10
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 derantell/4978086 to your computer and use it in GitHub Desktop.
Save derantell/4978086 to your computer and use it in GitHub Desktop.
C# substring extensions inspired by XPath's substring-before and substring-after functions.
public static class SubstringExtensions {
/// <summary>
/// Gets the substring after the first occurence of the specified character.
/// </summary>
/// <param name="self">The string self.</param>
/// <param name="ch">The character.</param>
/// <returns>The substring after the first occurrence of the specified character. If
/// The character does not occur in the string, the empty string is returned.</returns>
public static string SubstringAfter(this string self, char ch) {
int index = self == null ? -1 : self.IndexOf(ch);
return index > -1 ? self.Substring(index + 1) : string.Empty;
}
/// <summary>
/// Gets the substring after the first occurence of the specified string.
/// </summary>
/// <param name="self">The string self.</param>
/// <param name="str">The string to find.</param>
/// <returns>The substring after the first occurrence of the specified string. If
/// The string does not occur in the self, the empty string is returned.</returns>
public static string SubstringAfter(this string self, string str) {
int index = self.IndexOf(str);
if (index > -1) {
return self.Substring(index + str.Length);
}
return string.Empty;
}
/// <summary>
/// Gets the substring before the first occurrence of a character
/// </summary>
/// <param name="self">The self <see cref="string"/></param>
/// <param name="ch">The character</param>
/// <returns>The substring before the first occurrence of the specified character. If
/// The character does not occur in the self, the empty string is returned.</returns>
public static string SubstringBefore(this string self, char ch) {
int index = self.IndexOf(ch);
if (index > -1) {
return self.Substring(0, index);
}
return string.Empty;
}
/// <summary>
/// Gets the substring before the first occurrence of a string.
/// </summary>
/// <param name="self">The self <see cref="string"/></param>
/// <param name="str">The string to look for.</param>
/// <returns>The substring before the first occurrence of the specified string. If
/// The string does not occur in the self, the empty string is returned.</returns>
public static string SubstringBefore(this string self, string str) {
int index = self.IndexOf(str);
if (index > -1) {
return self.Substring(0, index);
}
return string.Empty;
}
/// <summary>
/// Gets the substring after the last occurrence of a character.
/// </summary>
/// <param name="self">The self <see cref="string"/></param>
/// <param name="ch">The character.</param>
/// <returns>The substring after the last occurrence of the specified character. If
/// The character does not occur in the self, the empty string is returned.</returns>
public static string SubstringAfterLast(this string self, char ch) {
int index = self.LastIndexOf(ch);
if (index > -1) {
return self.Substring(index + 1);
}
return string.Empty;
}
/// <summary>
/// Gets the substring after the last occurrence of a string.
/// </summary>
/// <param name="self">The self <see cref="string"/></param>
/// <param name="str">The string.</param>
/// <returns>The substring after the last occurrence of the specified string. If
/// The string does not occur in the self, the empty string is returned.</returns>
public static string SubstringAfterLast(this string self, string str) {
int index = self.LastIndexOf(str);
if (index > -1) {
return self.Substring(index + str.Length);
}
return string.Empty;
}
/// <summary>
/// Gets the substring before the last occurrence of a character.
/// </summary>
/// <param name="self">The self <see cref="string"/></param>
/// <param name="ch">The character.</param>
/// <returns>The substring before the last occurrence of the specified character. If
/// The character does not occur in the self, the empty string is returned.</returns>
public static string SubstringBeforeLast(this string self, char ch) {
int index = self.LastIndexOf(ch);
if (index > -1) {
return self.Substring(0, index);
}
return string.Empty;
}
/// <summary>
/// Gets the substring before the last occurrence of a string.
/// </summary>
/// <param name="self">The <see cref="string"/> self.</param>
/// <param name="str">The string.</param>
/// <returns>The substring after the last occurrence of the specified string. If
/// The string does not occur in the self, the empty string is returned.</returns>
public static string SubstringBeforeLast(this string self, string str) {
int index = self.LastIndexOf(str);
if (index > -1) {
return self.Substring(0, index);
}
return string.Empty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment