162

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?

001
  • 62,807
  • 94
  • 230
  • 350
  • possible duplicate of [Is there a way to distinguish a GUID from just a random number?](http://stackoverflow.com/questions/1771410/is-there-a-way-to-distinguish-a-guid-from-just-a-random-number) – sharptooth Jun 02 '11 at 06:12

10 Answers10

265

See if these helps :-

  1. Guid.Parse - Docs
Guid guidResult = Guid.Parse(inputString)
  1. Guid.TryParse - Docs
bool isValid = Guid.TryParse(inputString, out guidOutput)
Kols
  • 3,641
  • 2
  • 34
  • 42
Deepesh
  • 5,346
  • 6
  • 30
  • 45
  • 6
    .NET 4.0 : Second link...else, first link. +1 – Khepri Jun 02 '11 at 06:06
  • 8
    C# 7.0 use var or Guid or _ for inline out variables `bool isValid = Guid.TryParse(inputString, out var tempGuid);` or `bool isValid = Guid.TryParse(inputString, out _);` – xadriel Nov 06 '20 at 08:17
22

This is a fairly clean, modern C# approach that suppresses the out variable:

var isValid = Guid.TryParse(inputString, out _);
Jessica
  • 1,621
  • 2
  • 18
  • 34
16

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))
{
...
}
Doug Clutter
  • 3,646
  • 2
  • 29
  • 31
9

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.

tofutim
  • 22,664
  • 20
  • 87
  • 148
6

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;
    }
}
Can Gencer
  • 8,822
  • 5
  • 33
  • 52
3
if(MyGuid != Guid.Empty) 
{
   // Valid Guid
} 
else
{
   // Invalid Guid
}
Tagi
  • 302
  • 4
  • 17
Sameer Bahad
  • 555
  • 5
  • 4
2

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

if (!Guid.TryParse(yourGuidString, out yourGuid)){
          yourGuid= Guid.Empty;
}
maniac
  • 1,112
  • 1
  • 13
  • 19
2

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.

a4bike
  • 641
  • 1
  • 6
  • 12
1

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
xjfw34
  • 11
  • 3
  • 2
    Be aware that the question was made for C# language and the provided answer is in VB.Net. Would be nice to map it for C#. – Pimenta Apr 30 '20 at 00:18
1

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

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

Ross
  • 1,013
  • 14
  • 32