How to validate GUID is a GUID

后端 未结 9 1782
执念已碎
执念已碎 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))
    {
    ...
    }
    
    0 讨论(0)
  • 2020-12-23 16:24

    Will return the Guid if it is valid Guid, else it will return Guid.Empty

    if (!Guid.TryParse(yourGuidString, out yourGuid)){
              yourGuid= Guid.Empty;
    }
    
    0 讨论(0)
  • 2020-12-23 16:26

    Use GUID constructor standard functionality

    Public Function IsValid(pString As String) As Boolean
    
        Try
            Dim mGuid As New Guid(pString)
        Catch ex As Exception
            Return False
        End Try
        Return True
    
    End Function
    
    0 讨论(0)
提交回复
热议问题