Merge/Intertwine 2 strings of differing lengths

Viewed 194

I would like to intertwine(?) two strings, for example:

string A = 'HELLO WORLD!'
string B = '66666666666666666666' //twenty 6's
output = 'H6E6L6L6O6 6W6O6R6L6D6!666666666'

or for instance:

string A = 'SOME REALLY REALLY LONG STRING'
string B = '66666666666666666666'  //twenty 6's
output = 'S6O6M6E6 6R6E6A6L6L6Y6 6R6E6A6L6L6Y6 6L6ONG STRING'

Is there an inbuilt function for doing something like this, what is it called?

6 Answers

Perhaps map?

function joinIt(strs) {
  var strA = strs[0].length <= strs[1].length?strs[0]:strs[1];
  var strB = strs[1].length <= strs[0].length?strs[0]:strs[1];

  return strB.split("").map(function(b, i) {
    var a = strA.charAt(i);
    return b + a;
  }).join("")
}
console.log(
  joinIt(['HELLO WORLD!', '66666666666666666666'])
)

console.log(
  joinIt(['SOME REALLY REALLY LONG STRING','66666666666666666666'])
)  

There's no inbuilt function, but it's easy to do:

var a = 'some long string';
var b = '2292929292302720709970709710';

var str1 = a.length < b.length ? b:a;
var str2 = a.length < b.length ? a:b;

var result = [...str1].reduce((acc, char, index) => {
    acc += char + (str2[index] || '');
    return acc; 
}, '');

console.log(result);

There are no native JS functions, but it is a one-line function.

var stringA = 'SOME REALLY REALLY LONG STRING';
var stringB = '66666666666666666666'; //twenty 6's //twenty 6's
var stringC = '';

for (var i = 0; i < (Math.max(stringA.length, stringB.length)); i++) {
  stringC += (stringA[i] ? (stringB[i] ? stringA[i] + stringB[i] : stringA[i]) : (stringB[i] ? stringB[i] : ''));
}

console.log('stringC: ', stringC);

Using replace() you could pass the match to a callback function, then, using shift() you get the first element of the second string (turned into array) each time we have a match, at the end you add the remaining elements using + arr.join(""):

function addSomethig(str, str2){

    var arr = str2.split("");

    str = str.replace(/[A-Z ]/gi, (m)=>(arr.length>0)?m+arr.shift():m) + arr.join("");
    
    return str;

}

console.log(addSomethig('SOME REALLY REALLY LONG STRING', '66666666666666666666'));

console.log(addSomethig('HELLO WORLD!', '66666666666666666666'));

console.log(addSomethig('SOME REALLY REALLY LONG STRING', 'anything, like: 789798798798798'))

function interleave(str1, str2){
  let outstr = "";
  for(let i = 0; i < Math.max(str1.length, str2.length);i++){
    if(i < str1.length){
      outstr += str1[i];
    }
    if(i < str2.length){
      outstr += str2[i];
    }
  }
  return outstr;
}

console.log(interleave('aaaaa','bbbbbbbbbbbb'));

There is the built in String.raw template tag function which does almost exactly what you ask;

var strA = 'SOME REALLY REALLY LONG STRING',
    strB = '66666666666666666666',
    strZ = String.raw({raw : strA}, ...strB);

console.log(strZ);

So... coming to the almost part, if strA is shorter than strB the remaining characters of strB are skipped.

var strA = 'HELLO WORLD!',
    strB = '66666666666666666666',
    zipS = String.raw({raw: strA},...strB);
    
console.log(zipS);

In this case we can add a simple logic to fix our code.

var zipStr = (s,t,d) => ( d = s.length - t.length
                        , String.raw({raw: s},...t) + (d < 0 ? t.slice(d) : "")
                        ),
    strA   = 'HELLO WORLD!',
    strB   = 'SOME REALLY REALLY LONG STRING',
    strC   = '66666666666666666666',
    zipAC  = zipStr(strA,strC),
    zipBC  = zipStr(strB,strC);
    
console.log(zipAC);
console.log(zipBC);

Related