Diese Methode kontrolliert mittels Regex ob der übergebene String eine Guid ist.
benötigte Namespaces:
System.Text.RegularExpressions
/// <summary>
/// Determines whether the specified candidate is a valid GUID.
/// </summary>
/// <param name="candidate">The candidate.</param>
/// <returns>
/// <c>true</c> if the specified candidate is GUID; otherwise, <c>false</c>.
/// </returns>
public static bool IsValidGuid(string candidate)
{
if (string.IsNullOrEmpty(candidate))
return false;
Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$");
return isGuid.IsMatch(candidate);
}
4 Kommentare zum Snippet