This repository has been archived on 2024-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
thesis-src/Examples/Common/Extensions/StringExtensions.cs
2023-08-10 09:04:36 +02:00

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 };
}
}