/// <summary>
/// Compares wildcard to string
/// </summary>
/// <param name="WildString">String to compare</param>
/// <param name="Mask">Wildcard mask (ex: *.jpg)</param>
/// <returns>True if match found</returns>
public static bool CompareWildcard(string WildString, string Mask, bool IgnoreCase = true)
{
int i = 0, k = 0;
// Cannot continue with Mask empty
if (string.IsNullOrEmpty(Mask))
return false;
// If WildString is null -> make it an empty string
if (WildString == null)
WildString = string.Empty;
// If Mask is * and WildString isn't empty -> return true
if (string.Compare(Mask, "*") == 0 && !string.IsNullOrEmpty(WildString))
return true;
// If Mask is ? and WildString length is 1 -> return true
if (string.Compare(Mask, "?") == 0 && WildString.Length == 1)
return true;
// If WildString and Mask match -> no need to go any further
if (string.Compare(WildString, Mask, IgnoreCase) == 0)
return true;
while (k != WildString.Length)
{
switch (Mask[i])
{
case '*':
if ((i + 1) == Mask.Length)
return true;
while (k != WildString.Length)
{
if (CompareWildcard(WildString.Substring(k + 1), Mask.Substring(i + 1), IgnoreCase))
return true;
k += 1;
}
return false;
case '?':
break;
default:
if (IgnoreCase == false && WildString[k] != Mask[i])
return false;
if (IgnoreCase && Char.ToLower(WildString[k]) != Char.ToLower(Mask[i]))
return false;
break;
}
i += 1;
k += 1;
}
if (k == WildString.Length)
{
if (i == Mask.Length || Mask[i] == '*')
return true;
}
return false;
}
'C#' 카테고리의 다른 글
[C#] File Copy Access to the path error 1312 (0) | 2016.12.07 |
---|---|
[C#] txt파일 일부분 내용 수정 (0) | 2016.10.04 |
[C#] Error 0x80040154 (0) | 2016.09.22 |
[C#] 웹에서 ASPNET 버전 노출 방지 (0) | 2016.09.14 |
[C#] C,C++ dll 로드시 지정된 모듈을 찾을 수 없습니다. (Exception from HRESULT: 0x8007007E) (0) | 2016.08.31 |