0

For some reason I have been unable to find a quick solution to this. All I am trying to do is add dashes to a "randomly" generated 32 character string in Javascript like so:

From: AA681EBC64F642B1AFA95EE2A5D87350
To: AA681EBC - 64F6 - 42B1 - AFA9 - 5EE2A5D87350 (added spaces for clarity)

I assume the code would look something along the lines of

String.format(randomString, {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx});

but in Javascript

Current code (using angular-random-string.js):

var value = angular.uppercase(randomString(32));
4
  • How do you create random string? Could you post some code? Commented Sep 30, 2015 at 22:38
  • 1
    What have you tried? You say you can't find a solution. Does that mean you've tried to write one? If so, what issues did you encounter? Commented Sep 30, 2015 at 22:39
  • are you really asking this dup stackoverflow.com/questions/105034/… Commented Sep 30, 2015 at 22:41
  • Just attached what I'm using. I have been looking for a similar example that I posted and have not found anything. A way to define characters and then divide by dashes. I will try that example ergonaut. Looks like what I need and would actually allow me to remove the angular-random-string.js add on Commented Sep 30, 2015 at 22:45

2 Answers 2

1

You can use something like this: (fiddle here)

var code = "AA681EBC64F642B1AFA95EE2A5D87350";
var test = "AA681EBC-64F6-42B1-AFA9-5EE2A5D87350"
var fCode = code.substring(0, 8) + "-" + code.substring(8, 12) + "-" + code.substring(12, 16) + "-" + code.substring(16, 20) + "-" + code.substring(20);

// test
console.log(test === fCode);

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that's exactly what I was looking for. Thank you!
0

Using your existing function you could do this:

var parts = [randomString(8), randomString(4), randomString(4), randomString(4), randomString(12)];
var value = parts.join('-').toUpperCase();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.