OneBite.Dev - Coding blog in a bite size

Convert string to UUID format in Javascript

This is how to convert a 32 characters string into UUID format with - dash as separator on each 8 - 4 - 4 - 4 - 12 characters.

This is how to convert a 32 characters string into UUID format with - dash as separator on each 8 - 4 - 4 - 4 - 12 characters.

function insertString(str, index, value) {
    return str.substr(0, index) + value + str.substr(index);
}

function converToUUID(str){
 _uuid = insertString(_uuid, 8, '-')
 _uuid = insertString(_uuid, 13, '-')
 _uuid = insertString(_uuid, 18, '-')
 _uuid = insertString(_uuid, 23, '-')
 
 return _uuid
}

The idea is we insert a string on each of “X” characters. I use a simple insertString function that I found on stackoverflow answers.

The number 8, 13, 18, 23 is when the position of dash ”-” after we insert each one of them.

Online tool

javascript