How to validate GUID is a GUID

后端 未结 9 1781
执念已碎
执念已碎 2020-12-23 15:39

How to determine if a string contains a GUID vs just a string of numbers.

will a GUID always contain at least 1 alpha character?

相关标签:
9条回答
  • 2020-12-23 16:07

    A GUID is a 16-byte (128-bit) number, typically represented by a 32-character hexadecimal string. A GUID (in hex form) need not contain any alpha characters, though by chance it probably would. If you are targeting a GUID in hex form, you can check that the string is 32-characters long (after stripping dashes and curly brackets) and has only letters A-F and numbers.

    There is certain style of presenting GUIDs (dash-placement) and regular expressions can be used to check for this, e.g.,

    @"^(\{{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})$"
    

    from http://www.geekzilla.co.uk/view8AD536EF-BC0D-427F-9F15-3A1BC663848E.htm. That said, it should be emphasized that the GUID really is a 128-bit number and could be represented in a number of different ways.

    0 讨论(0)
  • 2020-12-23 16:07

    Based on the accepted answer I created an Extension method as follows:

    public static Guid ToGuid(this string aString)
    {
        Guid newGuid;
    
        if (string.IsNullOrWhiteSpace(aString))
        {
            return MagicNumbers.defaultGuid;
        }
    
        if (Guid.TryParse(aString, out newGuid))
        {
            return newGuid;
        }
    
        return MagicNumbers.defaultGuid;
    }
    

    Where "MagicNumbers.defaultGuid" is just "an empty" all zero Guid "00000000-0000-0000-0000-000000000000".

    In my case returning that value as the result of an invalid ToGuid conversion was not a problem.

    0 讨论(0)
  • 2020-12-23 16:13

    There is no guarantee that a GUID contains alpha characters. FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF is a valid GUID so is 00000000-0000-0000-0000-000000000000 and anything in between.

    If you are using .NET 4.0, you can use the answer above for the Guid.Parse and Guid.TryParse. Otherwise, you can do something like this:

    public static bool TryParseGuid(string guidString, out Guid guid)
    {
        if (guidString == null) throw new ArgumentNullException("guidString");
        try
        {
            guid = new Guid(guidString);
            return true;
        }
        catch (FormatException)
        {
            guid = default(Guid);
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-23 16:13

    see http://en.wikipedia.org/wiki/Globally_unique_identifier

    There is no guarantee that an alpha will actually be there.

    0 讨论(0)
  • 2020-12-23 16:13

    if(MyGuid!=Guild.Empty)

    {

    //Valid Guild

    }

    else {

    // Invalid Guild

    }

    0 讨论(0)
  • 2020-12-23 16:14

    See if these helps :-

    Guid guidResult = Guid.Parse(inputString)

    (http://msdn.microsoft.com/en-us/library/system.guid.parse.aspx)

    bool isValid = Guid.TryParse(inputString, out guidOutput)

    http://msdn.microsoft.com/en-us/library/system.guid.tryparse.aspx

    0 讨论(0)
提交回复
热议问题