請告訴我如何驗證.net中的GUID,它始終是唯一的?如何驗證.net中的Guid
回答
Guid's是獨一無二的99.9999999999999999999999999999999999%的時間。
這取決於你的意思是驗證嗎?
代碼,以確定一個GUID字符串,其實是一個GUID,是as follows:
private static Regex isGuid =
new Regex(@"^(\{){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}$", RegexOptions.Compiled);
internal static bool IsGuid(string candidate, out Guid output)
{
bool isValid = false;
output = Guid.Empty;
if(candidate != null)
{
if (isGuid.IsMatch(candidate))
{
output=new Guid(candidate);
isValid = true;
}
}
return isValid;
}
+1爲了避免使用異常,-1因爲'Guid '可以有其他格式,目前的方法會引發以下情況的例外:「{230a0f8b-81fb-4052-866e-9 ac6a7611c77「 – 2010-03-03 11:43:25
+1,在.NET 4.0中,我們終於可以看到'Guid.TryParse()'的引入,這顯然比使用正則表達式更可取。儘管如此,這是完全可以接受的。 http://connect.microsoft.com/VisualStudio/feedback/details/94072/guid-tryparse – 2010-03-03 11:43:32
Guid's不是隨機的,也許有一種方法可以檢查它們是否符合以下條件:http://blogs.msdn.com /b/oldnewthing/archive/2008/06/27/8659071.aspx – 2014-05-18 10:27:22
您無法驗證GUID的唯一性。你只希望它是用一個產生唯一的16字節的工具生成的。至於驗證,這個簡單的代碼可能工作(假設你正在處理的GUID的字符串表示:
bool ValidateGuid(string theGuid)
{
try { Guid aG = new Guid(theGuid); }
catch { return false; }
return true;
}
如果你正在尋找一種方式來確定它是否是一個實際的淨Guid
類型,take a look at this article的格式。一個快速的正則表達式就可以做到這一點:
2^128是一個非常非常大的數字,它比宇宙生命中的皮秒數大十億倍,通過一個長鏡頭來驗證太大,答案註定是「42」,這是使用它們的關鍵:你不必如果你擔心重複出現,你擔心錯誤的原因,你的機器將被流星imp破壞行爲相當大。
鴨子!
這個問題已經在this post中討論過了。您可能會發現更多有趣的細節
這裏有一個非正則表達式的答案應該是非常快:
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;
}
在.NET 4中,你可以使用這個擴展方法
public static class GuidExtensions
{
public static bool IsGuid(this string value)
{
Guid output;
return Guid.TryParse(value, out output);
}
}
- 1. 如何驗證GUID是一個GUID
- 2. Guid的驗證
- 3. SQL中的Guid驗證
- 4. 驗證Oracle列中的GUID值
- 5. 遠程驗證不是GUID
- 6. DLL中的GUID(.Net)
- 7. 如何在.NET中驗證LDAP
- 8. 如何在.NET中生成1版Guid?
- 9. .NET中的驗證MVC
- 10. 驗證.NET中的XPath
- 11. 如何驗證.NET集合的對象?
- 12. 如何使用net/http驗證ruby中的SSL證書鏈
- 13. 如何驗證.NET中HTTPS/SSL證書的有效性?
- 14. MVC3 .NET - 驗證
- 15. ASP .NET驗證
- 16. .net WinForms控件驗證 - 如何顯式驗證所有控件
- 17. 在.net中驗證數據
- 18. 在.NET中驗證XMLDSIG鏈?
- 19. Protobuf-net - 序列化.NET GUID - 如何在C++中讀取它?
- 20. Protobuf-Net:如何序列化guid?
- 21. NET Framework驗證庫
- 22. .NET驗證模式
- 23. 如何使用GUID來驗證ASP.NET中的用戶電子郵件#
- 24. 驗證/驗證在.NET中使用PHP創建的JWT令牌
- 25. .net和winforms的.Net身份驗證
- 26. 如何在.NET 2.0中驗證long的範圍?
- 27. 如何驗證C#/ .Net中的JPEG圖像是否已損壞
- 28. 如何使用.NET驗證內存模式中的XML文件?
- 29. 的Guid Costructor - 如何創建一個GUID驗證字符串傳遞是在正確的格式
- 30. 從.NET中的文件驗證數據
的GUID總是唯一如果所有的生成器都遵循以下規則:http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx – 2014-05-18 10:28:01