English 中文(简体)
如何在网上验证准则
原标题:How to validate Guid in .net
  • 时间:2010-03-03 11:19:19
  •  标签:
  • c#

请告诉我,如何在网内验证局,它总是独一无二的?

最佳回答
问题回答

2^128是一个非常庞大的数字。 这比宇宙生物体的生物体数量大10亿倍。 回答“42”。 什么是使用这些工具:你不需要。 如果你担心重复,那么你会因错误原因感到担忧。 贵机器的奇迹会因计量器的影响而毁损得多。

Duck!

这里的答案应当是相当迅速的:

public static bool IsHex(this char c)
{
    return ((c >=  0  && c <=  9 ) || (c >=  a  && c <=  f ) || (c >=  A  && c <=  F ));
}

public static bool IsGuid(this string s)
{
    // Length of a proper GUID, without any surrounding braces.
    const int len_without_braces = 36;

    // Delimiter for GUID data parts.
    const char delim =  - ;

    // Delimiter positions.
    const int d_0 = 8;
    const int d_1 = 13;
    const int d_2 = 18;
    const int d_3 = 23;

    // Before Delimiter positions.
    const int bd_0 = 7;
    const int bd_1 = 12;
    const int bd_2 = 17;
    const int bd_3 = 22;

    if (s == null)
        return false;

    if (s.Length != len_without_braces)
        return false;

    if (s[d_0] != delim ||
        s[d_1] != delim ||
        s[d_2] != delim ||
        s[d_3] != delim)
        return false;

    for (int i = 0;
        i < s.Length;
        i = i + (i == bd_0 ||
                i == bd_1 ||
                i == bd_2 ||
                i == bd_3
                ? 2 : 1))
    {
        if (!IsHex(s[i])) return false;
    }

    return true;
}

你们不能肯定古董会的独特性。 你们只是希望它产生于一种工具,它产生独特的16个 by子。 关于验证,这一简单法典可能发挥作用(假设你正在处理国际统一分类法的表述:

bool ValidateGuid(string theGuid)
{
  try { Guid aG = new Guid(theGuid); }
  catch { return false; }

  return true;
}

在网4中,你可以使用这一推广方法。

public static class GuidExtensions
    {
        public static bool IsGuid(this string value)
        {
            Guid output;
            return Guid.TryParse(value, out output);
        }
    }

i 写了延期。

public static bool TryParseGuid(this Guid? guidString)
       {
           if (guidString != null && guidString != Guid.Empty)
           {
               if (Guid.TryParse(guidString.ToString(), out _))
               {
                   return true;
               }
               else
                   return false;
           }

           return false;
       }

如果你重新寻找一种方法,以确定它是否符合实际格式。 http://web.archive.org/web/20130818121/http://madskristensen.net/post/Validate-a-GUID-in-C.aspx”rel=“nofollow noretinger” 速效.。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签