如何验证 GUID 是否为 GUID

发布于 2024-11-11 15:31:12 字数 67 浏览 4 评论 0原文

如何确定字符串是否包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

宫墨修音 2024-11-18 15:31:12

看看这些是否有帮助:-

  1. Guid.Parse - 文档
Guid guidResult = Guid.Parse(inputString)
  1. Guid.TryParse - 文档
bool isValid = Guid.TryParse(inputString, out guidOutput)

See if these helps :-

  1. Guid.Parse - Docs
Guid guidResult = Guid.Parse(inputString)
  1. Guid.TryParse - Docs
bool isValid = Guid.TryParse(inputString, out guidOutput)
夏有森光若流苏 2024-11-18 15:31:12

这是一种相当干净、现代的 C# 方法,可以抑制 out 变量:

var isValid = Guid.TryParse(inputString, out _);

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

var isValid = Guid.TryParse(inputString, out _);
娇妻 2024-11-18 15:31:12

当我只是测试一个字符串以查看它是否是 GUID 时,我真的不想创建一个我不需要的 Guid 对象。所以......

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

这是你如何使用它:

string testMe = "not a guid";
if (GuidEx.IsGuid(testMe))
{
...
}

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))
{
...
}
锦欢 2024-11-18 15:31:12

GUID 是一个 16 字节(128 位)数字,通常由 32 个字符的十六进制字符串表示。 GUID(十六进制形式)不需要包含任何字母字符,尽管偶然它可能会包含。如果您的目标是十六进制形式的 GUID,则可以检查该字符串的长度是否为 32 个字符(去掉破折号和大括号后)并且仅包含字母 AF 和数字。

有某种呈现 GUID 的风格(破折号位置),并且可以使用正则表达式来检查这一点,例如

@"^(\{{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})$"

来自 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.,

@"^(\{{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.

秋叶绚丽 2024-11-18 15:31:12

不保证 GUID 包含字母字符。 FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF 是有效的 GUID,因此 00000000-0000-0000-0000-000000000000 以及介于两者之间的任何内容都是有效的 GUID。

如果您使用的是 .NET 4.0,则可以使用上面的 Guid.Parse 和 Guid.TryParse 答案。否则,你可以这样做:

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;
    }
}

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;
    }
}
痴梦一场 2024-11-18 15:31:12

如果 Guid 有效则返回 Guid,否则返回 Guid.Empty

if (!Guid.TryParse(yourGuidString, out yourGuid)){
          yourGuid= Guid.Empty;
}

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

if (!Guid.TryParse(yourGuidString, out yourGuid)){
          yourGuid= Guid.Empty;
}
素年丶 2024-11-18 15:31:12
if(MyGuid != Guid.Empty) 
{
   // Valid Guid
} 
else
{
   // Invalid Guid
}
if(MyGuid != Guid.Empty) 
{
   // Valid Guid
} 
else
{
   // Invalid Guid
}
空‖城人不在 2024-11-18 15:31:12

请参阅 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.

盗琴音 2024-11-18 15:31:12

根据接受的答案,我创建了一个扩展方法,如下所示:

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;
}

其中“MagicNumbers.defaultGuid”只是“空”全零 Guid“00000000-0000-0000-0000-000000000000”。

就我而言,由于无效的 ToGuid 转换而返回该值不是问题。

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.

小傻瓜 2024-11-18 15:31:12

使用 GUID 构造函数标准功能

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文