Skip to content

Instantly share code, notes, and snippets.

@lucasloss
Created December 19, 2019 22:21
Show Gist options
  • Save lucasloss/95ae43220e808657f2b53f3cb5c84a68 to your computer and use it in GitHub Desktop.
Save lucasloss/95ae43220e808657f2b53f3cb5c84a68 to your computer and use it in GitHub Desktop.
System.String extension methods.
using System;
using System.IO;
namespace ExtensionMethods
{
/// <summary>
/// String extension methods.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Gets the <see cref="Enum"/> equivalent of the <see cref="string"/>.
/// </summary>
/// <typeparam name="T">The type of the <see cref="Enum"/>.</typeparam>
/// <param name="value">The <see cref="string"/>.</param>
/// <param name="caseSensitive">Indicates whether the operation is case-sensitive. Default is true.</param>
/// <returns>The <see cref="Enum"/> of T.</returns>
public static T ToEnum<T>(this string value, bool caseSensitive = true)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return (T)Enum.Parse(typeof(T), value, caseSensitive);
}
/// <summary>
/// Returns the number of lines of the <see cref="string"/>.
/// This extension method uses the <see cref="Environment.NewLine"/> for counting the number of lines.
/// </summary>
/// <param name="value">The <see cref="string"/>.</param>
/// <returns>Number of lines of the <see cref="string"/>.</returns>
public static int CountLines(this string value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (value == string.Empty)
{
return 0;
}
int index = -1;
int count = 0;
while ((index = value.IndexOf(Environment.NewLine, index + 1)) != -1)
{
count++;
}
return count + 1;
}
/// <summary>
/// Returns true if <paramref name="path"/> starts with the path <paramref name="baseDirPath"/>.
/// The comparison is case-insensitive, handles / and \ slashes as folder separators and
/// only matches if the base dir folder name is matched exactly ("c:\foobar\file.txt" is not a sub path of "c:\foo").
/// </summary>
/// <param name="path">The path that might be a sub path of another.</param>
/// <param name="baseDirPath">The base path.</param>
/// <returns>Returns true if <paramref name="path"/> starts with the path <paramref name="baseDirPath"/>.</returns>
public static bool IsSubPathOf(this string path, string baseDirPath)
{
// Source: https://stackoverflow.com/questions/5617320/given-full-path-check-if-path-is-subdirectory-of-some-other-path-or-otherwise.
string normalizedPath = Path.GetFullPath(path.Replace('/', '\\').WithEnding("\\"));
string normalizedBaseDirPath = Path.GetFullPath(baseDirPath.Replace('/', '\\').WithEnding("\\"));
return normalizedPath.StartsWith(normalizedBaseDirPath, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns <paramref name="str"/> with the minimal concatenation of <paramref name="ending"/> (starting from end) that
/// results in satisfying .EndsWith(ending).
/// </summary>
/// <param name="str">The string.</param>
/// <param name="ending">The ending.</param>
/// <returns>The resulting string.</returns>
public static string WithEnding(this string str, string ending)
{
// Source: https://stackoverflow.com/questions/5617320/given-full-path-check-if-path-is-subdirectory-of-some-other-path-or-otherwise.
if (str == null)
{
return ending;
}
string result = str;
// Right() is 1-indexed, so include these cases
// * Append no characters
// * Append up to N characters, where N is ending length
for (int i = 0; i <= ending.Length; i++)
{
string tmp = result + ending.Right(i);
if (tmp.EndsWith(ending))
{
return tmp;
}
}
return result;
}
/// <summary>
/// Gets the rightmost <paramref name="length" /> characters from a string.
/// </summary>
/// <param name="value">The string to retrieve the substring from.</param>
/// <param name="length">The number of characters to retrieve.</param>
/// <returns>The substring.</returns>
public static string Right(this string value, int length)
{
// Source: https://stackoverflow.com/questions/5617320/given-full-path-check-if-path-is-subdirectory-of-some-other-path-or-otherwise.
if (value == null)
{
throw new ArgumentNullException("value");
}
if (length < 0)
{
throw new ArgumentOutOfRangeException("length", length, "Length is less than zero");
}
return (length < value.Length) ? value.Substring(value.Length - length) : value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment