/// <summary> /// Determines whether the specified <paramref name="value"/> has a valid format of a <see cref="Guid"/>. /// </summary> /// <param name="value">The string to verify has a valid format of a <see cref="Guid"/>.</param> /// <param name="format">A bitmask comprised of one or more <see cref="GuidFormats"/> that specify how the GUID parsing is conducted.</param> /// <returns><c>true</c> if the specified <paramref name="value"/> has a format of a <see cref="Guid"/>; otherwise, <c>false</c>.</returns> public static bool IsGuid(string value, GuidFormats format) { if (string.IsNullOrEmpty(value)) { return(false); } Guid result; return(GuidUtility.TryParse(value, format, out result)); }
/// <summary> /// Converts the string representation of a GUID to its equivalent <see cref="Guid"/> structure. /// </summary> /// <param name="value">The GUID to convert.</param> /// <param name="format">A bitmask comprised of one or more <see cref="GuidFormats"/> that specify how the GUID parsing is conducted.</param> /// <param name="result">The structure that will contain the parsed value.</param> /// <returns><c>true</c> if the parse operation was successful; otherwise, <c>false</c>.</returns> /// <remarks> /// This method returns <c>false</c> if <paramref name="value"/> is <c>null</c> or not in a recognized format, and does not throw an exception.<br/> /// Only 32 digits (<see cref="GuidFormats.NumberFormat"/>); 32 digits separated by hyphens (<see cref="GuidFormats.DigitFormat"/>); 32 digits separated by hyphens, enclosed in brackets (<see cref="GuidFormats.BraceFormat"/>) and 32 digits separated by hyphens, enclosed in parentheses (<see cref="GuidFormats.ParenthesisFormat"/>) is supported.<br/> /// For more information refer to this page @ StackOverflow: http://stackoverflow.com/questions/968175/what-is-the-string-length-of-a-guid /// </remarks> public static bool TryParse(string value, GuidFormats format, out Guid result) { result = Guid.Empty; if (string.IsNullOrEmpty(value)) { return(true); } if (value.Length < 32) { return(false); } if (value.Length > 38) { return(false); } int startPosition = 0; bool hasDashes = (value.IndexOf('-') > 0); bool hasBraces = (value.StartsWith("{", StringComparison.OrdinalIgnoreCase) && value.EndsWith("}", StringComparison.OrdinalIgnoreCase)); bool hasParenthesis = (value.StartsWith("(", StringComparison.OrdinalIgnoreCase) && value.EndsWith(")", StringComparison.OrdinalIgnoreCase)); if (hasBraces || hasParenthesis) // BraceFormat or ParenthesisFormat { if (!EnumUtility.HasFlag(format, GuidFormats.BraceFormat) && !EnumUtility.HasFlag(format, GuidFormats.ParenthesisFormat)) { return(false); } if (value.Length != 38) { return(false); } startPosition = 1; } if (hasDashes) // DigitFormat { if (!EnumUtility.HasFlag(format, GuidFormats.DigitFormat) && startPosition == 0) { return(false); } if ((!EnumUtility.HasFlag(format, GuidFormats.BraceFormat) && startPosition == 1) && (!EnumUtility.HasFlag(format, GuidFormats.ParenthesisFormat) && startPosition == 1)) { return(false); } if (value.Length != 36 && startPosition == 0) { return(false); } if (value[startPosition + 8] != '-' || value[startPosition + 13] != '-' || value[startPosition + 18] != '-' || value[startPosition + 23] != '-') { return(false); } } else // NumberFormat { if (!EnumUtility.HasFlag(format, GuidFormats.NumberFormat)) { return(false); } if (value.Length != 32) { return(false); } } if (!IsCharWiseValid(value, hasDashes, hasParenthesis, hasBraces)) { return(false); } try { result = new Guid(value); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Determines whether the specified <paramref name="value"/> has a valid format of a <see cref="Guid"/>. /// </summary> /// <param name="value">The string to verify has a valid format of a <see cref="Guid"/>.</param> /// <param name="format">A bitmask comprised of one or more <see cref="GuidFormats"/> that specify how the GUID parsing is conducted.</param> /// <returns><c>true</c> if the specified <paramref name="value"/> has a format of a <see cref="Guid"/>; otherwise, <c>false</c>.</returns> public static bool IsGuid(this string value, GuidFormats format) { return(Condition.IsGuid(value, format)); }