How to know if a string is a guid

  • Thread starter Thread starter ucasesoftware
  • Start date Start date
ucasesoftware said:
How to know if a string is a guid

I suppose you culd try and construct a System.Guid from it, and see if
that throws a FormatException, but that's a little inelegant. Can't see
any other readily available method though.
 
ucasesoftware said:
How to know if a string is a guid

For example:

\\\
Try
Dim g As New Guid(<string>)
Catch ex As FormatException
...
End Try
///
 
Hi,

I would use an regular expression.

Dim regGuid As New
Regex("^\{?[a-fA-F\d]{8}-([a-fA-F\d]{4}-){3}[a-fA-F\d]{12}\}?$")

Dim strTest As String = Guid.NewGuid.ToString
Trace.WriteLine(regGuid.IsMatch(strTest))
Trace.WriteLine(regGuid.IsMatch("No way"))


Ken
 
ucasesoftware,
I would normally use the method Herfried suggests:

Try
Dim g As New Guid(<string>)
Catch ex As FormatException
...
End Try

I was going to suggest TryParse with VB 2005, however I don't see it listed
for Guid, just the "numeric" types (Integer, Long, Single, Double, Decimal,
DateTime...) possibly because Guid doesn't have a Parse function...

If I considered the RegEx approach as Ken suggests, I would be certain to
take into account all 5 formats that a GUID could be in:

http://msdn.microsoft.com/library/d...cpref/html/frlrfSystemGuidClassctorTopic2.asp

Ergo I suspect the Try/Catch would be "easier" in the long run, although the
RegEx approach might be "Better" for dialog boxes...

--
Hope this helps
Jay
T.S. Bradley - http://www.tsbradley.net


| How to know if a string is a guid
|
| thx
|
 
Jay,
I would normally use the method Herfried suggests:
Just before somebody gets wrong idea from you, from what I am sure is not
your intention.

In my opinion Larry and Hefried, they posted in my opinion at about the same
time almost the same solution.

Cor
 
Cor,
| Just before somebody gets wrong idea from you, from what I am sure is not
| your intention.
Huh?

| In my opinion Larry and Hefried, they posted in my opinion at about the
same
| time almost the same solution.
Yes Larry & Herfried gave the "same" technique, however only Herfried gave
sample code. I was giving Herfried credit for using his code.

--
Hope this helps
Jay
T.S. Bradley - http://www.tsbradley.net


| Jay,
|
| > I would normally use the method Herfried suggests:
| >
| Just before somebody gets wrong idea from you, from what I am sure is not
| your intention.
|
| In my opinion Larry and Hefried, they posted in my opinion at about the
same
| time almost the same solution.
|
| Cor
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top