How to check a GUID is a GUID

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

will the GUID always contain at least 1 alpha character?

+43
string c # guid
Jun 02 2018-11-11T00:
source share
8 answers

See if this 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

+72
Jun 02 2018-11-11T00:
source share

A GUID is a 16-byte (128-bit) number, usually represented by a 32-character hexadecimal string. The GUID (in hexadecimal form) should not contain any alpha characters, although this is probably possible. If you target the GUID in hexadecimal, you can verify that the string is 32 characters long (after removing dashes and curly braces) and only has AF letters and numbers.

There is a certain GUID (dash-placement) presentation style, and regular expressions can be used to verify this, for example,

 @"^(\{{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 . It should be emphasized that the GUID is indeed a 128-bit number and can be represented in several different ways.

+5
Jun 02 2018-11-11T00:
source share

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

If you are using .NET 4.0, you can use the answer above for 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; } } 
+5
Jun 02 '11 at 6:12
source share

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 "empty" all zero Guid "00000000-0000-0000-0000-000000000000".

In my case, returning this value as a result of an incorrect ToGuid conversion was not a problem.

+2
Mar 19 '16 at 18:42
source share

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

There is no guarantee that there will actually be alpha.

+1
Jun 2 2018-11-11T00:
source share

Will return Guid if it is valid Guid, otherwise it will return Guid.Empty

 if (!Guid.TryParse(yourGuidString, out yourGuid)){ yourGuid= Guid.Empty; } 
0
Dec 16 '14 at 12:53 on
source share

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

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

And here is how you use it:

 string testMe = "not a guid"; if (GuidEx.IsGuid(testMe)) { ... } 
0
May 12 '16 at 19:59
source share

you can also use regular expression, look at this site and do a guid search. http://regexlib.com

-3
Jun 02 2018-11-11T00:
source share



All Articles