li.的博客
li.的首页 > li.的博客 > 浏览文章

判断字符串是否能转化成Guid

分类:.net应用  人气:1532  评论:0  时间:2011-03-09 10:15
string s  =  null;
Guid  gv  =  Guid.Empty;
try
{
gv  =  new  Guid(s);
}
catch
{

}
if   (gv  !=  Guid.Empty)
{
//转换成功
}
 
===================================
GuidConverter   gc   =   new   GuidConverter();
gc.ConvertFrom(str);
 
============================
没有“-”也是允许的
@ "^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$|^[0-9a-f]{32}$ ",
 
====================================
private bool IsGuid(string strToValidate)
       {
           bool isGuid = false;
           string strRegexPatten = @"^(\{){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}$";
           if (strToValidate != null && !strToValidate.Equals(""))
           {
               isGuid =System.Text.RegularExpressions.Regex.IsMatch(strToValidate, strRegexPatten);
           }
           return isGuid;
       }
评论(0)
暂无评论
我来评论
(800字以内)