41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Common.Extensions;
|
|
|
|
/// <summary>
|
|
/// Extensions for the string object type
|
|
/// </summary>
|
|
public static class StringExtensions
|
|
{
|
|
private static readonly Regex patternRegex = new Regex(@"^\*$");
|
|
|
|
/// <summary>
|
|
/// Determines whether this string contains the specified string. Not case sensitive.
|
|
/// </summary>
|
|
/// <param name="source"> The source.</param>
|
|
/// <param name="contained">The contained.</param>
|
|
public static bool ContainsIgnoreCase(this string source, string contained)
|
|
{
|
|
return source?.IndexOf(contained, StringComparison.InvariantCultureIgnoreCase) >= 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Expands a path containing a wildcard pattern
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <returns></returns>
|
|
public static ICollection<string> ExpandPath(this string self)
|
|
{
|
|
string pattern = Path.GetFileName(self);
|
|
if (patternRegex.IsMatch(pattern))
|
|
{
|
|
return Directory.GetFiles(
|
|
self.Substring(0, self.Length - pattern.Length),
|
|
pattern,
|
|
SearchOption.TopDirectoryOnly
|
|
);
|
|
}
|
|
|
|
return new[] { self };
|
|
}
|
|
} |