如何验证 GUID 是否为 GUID
如何确定字符串是否包含 GUID 还是仅包含数字字符串。
GUID 是否始终包含至少 1 个字母字符?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
看看这些是否有帮助:-
Guid.Parse
- 文档Guid.TryParse
- 文档See if these helps :-
Guid.Parse
- DocsGuid.TryParse
- Docs这是一种相当干净、现代的 C# 方法,可以抑制 out 变量:
This is a fairly clean, modern C# approach that suppresses the out variable:
当我只是测试一个字符串以查看它是否是 GUID 时,我真的不想创建一个我不需要的 Guid 对象。所以......
这是你如何使用它:
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...
And here's how you use it:
GUID 是一个 16 字节(128 位)数字,通常由 32 个字符的十六进制字符串表示。 GUID(十六进制形式)不需要包含任何字母字符,尽管偶然它可能会包含。如果您的目标是十六进制形式的 GUID,则可以检查该字符串的长度是否为 32 个字符(去掉破折号和大括号后)并且仅包含字母 AF 和数字。
有某种呈现 GUID 的风格(破折号位置),并且可以使用正则表达式来检查这一点,例如
来自 http://www.geekzilla.co.uk/view8AD536EF-BC0D-427F-9F15-3A1BC663848E.htm。也就是说,应该强调的是,GUID 实际上是一个 128 位数字,可以用多种不同的方式表示。
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.,
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.
不保证 GUID 包含字母字符。
FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF
是有效的 GUID,因此00000000-0000-0000-0000-000000000000
以及介于两者之间的任何内容都是有效的 GUID。如果您使用的是 .NET 4.0,则可以使用上面的 Guid.Parse 和 Guid.TryParse 答案。否则,你可以这样做:
There is no guarantee that a GUID contains alpha characters.
FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF
is a valid GUID so is00000000-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:
如果 Guid 有效则返回 Guid,否则返回 Guid.Empty
Will return the Guid if it is valid Guid, else it will return Guid.Empty
请参阅 http://en.wikipedia.org/wiki/Globally_unique_identifier
不保证 alpha实际上会在那里。
see http://en.wikipedia.org/wiki/Globally_unique_identifier
There is no guarantee that an alpha will actually be there.
根据接受的答案,我创建了一个扩展方法,如下所示:
其中“MagicNumbers.defaultGuid”只是“空”全零 Guid“00000000-0000-0000-0000-000000000000”。
就我而言,由于无效的 ToGuid 转换而返回该值不是问题。
Based on the accepted answer I created an Extension method as follows:
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.
使用 GUID 构造函数标准功能
Use GUID constructor standard functionality