function getUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
帮忙 转成c#,谢谢。
public static string getUID()
{
var str = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
var reg = new Regex("[xy]");
var random = new Random();
while (reg.IsMatch(str))
{
var match = reg.Match(str);
var ran = random.Next(0, 16);
var temp = match.Value == "x" ? ran : (ran & 0x3 | 0x8);
str = reg.Replace(str, temp.ToString(), 1);
}
return str;
}
这个样子会不会有重复的啊?C#里面直接有GUID的方法吧
这个是JS的GUID生产算法
https://www.cnblogs.com/snandy/p/3261754.html
所以c#没必要写了,直接GUID类
Guid.NewGuid().ToString()
return Guid.NewGuid()