How to validate GUID is a GUID

后端 未结 9 1808
执念已碎
执念已碎 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:15

    When I'm just testing a string to see if it is a GUID, I don't really want to create a Guid object that I don't need. So...

    public static class GuidEx
    {
        public static bool IsGuid(string value)
        {
            Guid x;
            return Guid.TryParse(value, out x);
        }
    }
    

    And here's how you use it:

    string testMe = "not a guid";
    if (GuidEx.IsGuid(testMe))
    {
    ...
    }
    

提交回复
热议问题