0

I have a string, which always has a lenght of 32:

var str = "34v188d9cefa401f988563fb153xy04b";

Now I need to add a minus (-) after following number of characters:

  • First minus after 8th character
  • Second minus after 12th character
  • Third minus after 16th character
  • Fourth minus after 20th character

Output should be:

34v188d9-cefa-401f-9885-63fb153xy04b

So far I have tried different calculations with e.g.:

str.split('').reduce((a, b, c) => a + b + (c % 6 === 4 ? '-' : ''), '');

But I don't get the expected result.

rimed
  • 105
  • 7

7 Answers7

2

You could use a regex replacement:

var str = "34v188d9cefa401f988563fb153xy04b";
var output = str.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
console.log(output);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If you want to use reduce you can do something like this

var str = "34v188d9cefa401f988563fb153xy04b";


const result = str.split('').reduce((res, c, i) => {
  const hiphensPositions = [7, 11, 15, 19]
  return res + c + (hiphensPositions.includes(i)?'-':'')
}, '')

console.log(result)
R4ncid
  • 6,944
  • 1
  • 4
  • 18
0
     var str = "34v188d9cefa401f988563fb153xy04b";
     var a="" 
     for (let i in str){if (i == 7 || i==11 ||  i==15  ||  i == 
      19){a = a+str[i]+"-"} else{a+=str[i] }}

should do it.

0

You can splice them.

const str = "34v188d9cefa401f988563fb153xy04b";
const indices = [8,12,16,20]
let strArr = str.split('')
indices.reverse().forEach(i => strArr.splice(i,0,'-'))
console.log(strArr.join(''))
holydragon
  • 6,158
  • 6
  • 39
  • 62
0

You can use the substring method

var str = '34v188d9cefa401f988563fb153xy04b';

var newStr =
    str.substring(0, 8) +
    '-' +
    str.substring(8, 12) +
    '-' +
    str.substring(12, 16) +
    '-' +
    str.substring(16, 20) +
    '-' +
    str.substring(20);

console.log(newStr);
0

I have a for loop algorithm for this case. It take O(n) complexity. The code example is like this:

const str = "34v188d9cefa401f988563fb153xy04b"
let dashPosition = [8, 12, 16, 20]

for(i=arr.length-1 ; i>=0 ; i--) {
    str = str.substr(0, [arr[i]]) + '-' + str.substr(arr[i])
}

Is the answer match what you want?

0

You can do this with splice and a loop.

The loop iterates all the positions you want to add a -, and uses the index to ensure that each time you add a new character to the string, the required positionto insert the - is also incremented.

const str = "34v188d9cefa401f988563fb153xy04b";
const positions = [8, 12, 16, 20];

const strChars = str.split("");
for(const [index, position] of positions.entries()) {
  strChars.splice(position + index, 0, "-");
}
const output = strChars.join("");

console.log(output); // expected output: "34v188d9-cefa-401f-9885-63fb153xy04b"

You could wrap this up in a function that just takes the string and positions array like so:

function insertHyphens(str, positions) {
  const strChars = str.split("");
  for(const [index, position] of positions.entries()) {
    strChars.splice(position + index, 0, "-");
  }
  return strChars.join("");
}

console.log(insertHyphens("34v188d9cefa401f988563fb153xy04b", [8, 12, 16, 20])); 
// expected output: "34v188d9-cefa-401f-9885-63fb153xy04b"