C# 判断字符串是否可以转为Guid 字符串转Guid
如何判断一下字符串是不是能转化成guid
/// <summary> /// 判断字符串是否可以转为Guid /// </summary> /// <param name="str">字符串</param> /// <returns></returns> public bool IfStr2Guid(string str) { bool IsCan = false; str = str.ToLower();//可以不用 //var regexStr1 = string.Format(@"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$|^[0-9a-f]{32}$ ");//允许没有'-' //var regexStr2 = string.Format(@"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$ "); Match m = Regex.Match(str, @"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$|^[0-9a-f]{32}$ ", RegexOptions.IgnoreCase);//忽略大小写 if (m.Success) { IsCan = true; } return IsCan; }
斩后知