Javascript 字符串到 Guid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25131143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:00:55  来源:igfitidea点击:

Javascript string to Guid

javascriptjquerystringguid

提问by Linc Abela

in javascript, what is the easiest way to convert this string

在javascript中,转换此字符串的最简单方法是什么

798205486e954fa880a0b366e6725f71

to GUID format like this

像这样的 GUID 格式

79820548-6e95-4fa8-80a0-b366e6725f71

this is the messy way I do it :) im looking for the cleanest way

这是我这样做的凌乱方式:) 我正在寻找最干净的方式

var employeeId = shift.employee.id.substring(0, 8) + "-" + shift.employee.id.substring(8, 12)
                    + "-" + shift.employee.id.substring(12, 16) + "-" + shift.employee.id.substring(16, 20) + "-" + shift.employee.id.substring(20, 32);

回答by Mritunjay

I did it in string manipulation

我是在字符串操作中做到的

var str = "798205486e954fa880a0b366e6725f71";
var parts = [];
parts.push(str.slice(0,8));
parts.push(str.slice(8,12));
parts.push(str.slice(12,16));
parts.push(str.slice(16,20));
parts.push(str.slice(20,32));
var GUID = parts.join('-'); 

console.log(GUID) // prints expected GUID

I did it this way because I don't like inserting characters between strings. If there is any problem tell me.

我这样做是因为我不喜欢在字符串之间插入字符。如果有任何问题告诉我。

Or you could use a forloop like bellow

或者你可以使用for像波纹管这样的循环

var str = "798205486e954fa880a0b366e6725f71";
var lengths = [8,4,4,4,12];
var parts = [];
var range = 0; 
for (var i = 0; i < lengths.length; i++) {
    parts.push(str.slice(range,range+lengths[i]));
    range += lengths[i];
};
var GUID = parts.join('-');
console.log(GUID);

回答by u283863

Cleanest way?

最干净的方式?

Shortest:

最短:

var txt = shift.employee.id;
txt.replace(/([0-z]{8})([0-z]{4})([0-z]{4})([0-z]{4})([0-z]{12})/,"----");
//"79820548-6e95-4fa8-80a0-b366e6725f71"

or if you don't care about the acceptable characters, it can be be even shorter (and cleaner):

或者如果你不关心可接受的字符,它可以更短(更干净):

txt.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/,"----");  //boom!

Some don't like using regex for everything, but I liked it.

有些人不喜欢对所有事情都使用正则表达式,但我喜欢它。

回答by Useless Code

You could use an regular expression:

您可以使用正则表达式:

var rxGetGuidGroups = /(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/,
    employeeId = shift.employee.id.replace(rxGetGuidGroups, '----');

jsFiddle

js小提琴

回答by Ankush Jain

Try this function, It will return string in GUID format

试试这个函数,它会以 GUID 格式返回字符串

function getGuid(str){
return str.slice(0,8)+"-"+str.slice(8,12)+"-"+str.slice(12,16)+
"-"+str.slice(16,20)+"-"+str.slice(20,str.length+1)
}

回答by user3163213

Or you would try-

或者你会尝试-

var guid = (function () {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
               .toString(16)
               .substring(1);
    }
    return function () {
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
           s4() + '-' + s4() + s4() + s4();
    };
})();

WhereYour new guid be like-

你的新向导在哪里——

var newGuid= guid();

newGuid returns- 7d4b3ef0-b5bb-5c42-2a02-80a4371babf8

newGuid 返回- 7d4b3ef0-b5bb-5c42-2a02-80a4371babf8