最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Doubling each letter in a String in js - Stack Overflow

programmeradmin4浏览0评论

I need string Double each letter in a string

abc -> aabbcc

i try this

var s = "abc";
for(var i = 0; i < s.length  ; i++){
   console.log(s+s);
}

o/p

>     abcabc    
>     abcabc  
>     abcabc

but i need

aabbcc

help me

I need string Double each letter in a string

abc -> aabbcc

i try this

var s = "abc";
for(var i = 0; i < s.length  ; i++){
   console.log(s+s);
}

o/p

>     abcabc    
>     abcabc  
>     abcabc

but i need

aabbcc

help me

Share Improve this question asked Nov 1, 2016 at 10:16 ArthiArthi 1,0044 gold badges12 silver badges36 bronze badges 0
Add a ment  | 

11 Answers 11

Reset to default 8

Use String#split , Array#map and Array#join methods.

var s = "abc";

console.log(
  // split the string into individual char array
  s.split('').map(function(v) {
    // iterate and update
    return v + v;
    // join the updated array
  }).join('')
)


UPDATE : You can even use String#replace method for that.

var s = "abc";

console.log(
  // replace each charcter with repetition of it
  // inside substituting string you can use $& for getting matched char
  s.replace(/./g, '$&$&')
)

You need to reference the specific character at the index within the string with s[i] rather than just s itself.

var s = "abc";
var out = "";
for(var i = 0; i < s.length  ; i++){
   out = out + (s[i] + s[i]);
}

console.log(out);

I have created a function which takes string as an input and iterate the string and returns the final string with each character doubled.

var s = "abcdef";

function makeDoubles(s){

  var s1 = "";
  for(var i=0; i<s.length; i++){
    s1 += s[i]+s[i];
  }
  return s1;
  
}

alert(makeDoubles(s));

if you want to make it with a loop, then you have to print s[i]+s[i]; not, s + s.

var s = "abc";
let newS = "";
for (var i = 0; i < s.length; i++) {
  newS += s[i] + s[i];
}

console.log(newS);

that works for me, maybe a little bit hardcoded, but I am new too)) good luck

console.log(s+s);, here s holds entire string. You will have to fetch individual character and append it.

var s = "abc";
var r = ""
for (var i = 0; i < s.length; i++) {
  var c = s.charAt(i);
  r+= c+c
}

console.log(r)

var doubleStr = function(str) {
    str = str.split('');
    var i = 0;

    while (i < str.length) {
        str.splice(i, 0, str[i]);
        i += 2;
    }

    return str.join('');
};

We loop through the characters of the input, duplicating it and appending it to an output that was initialized as an empty string.

function doubleChar(str) {
  let s = str;
  let s1 = "";
  let result = "";
  
  for(var i=0; i<s.length; i++){
    s1 = s[i] + s[i];
    result += s1;
  }
  return result;
}
console.log(doubleChar("abc"));

This is achievable by bining split, join and template literals:

function output(value) {
    return value.split("").map(item => `${item}${item}`).join('');
}
<input type="text" oninput="console.log(output(this.value))">

A generic function to repeat all characters in a string [n] times, using String.prototype.repeat. Check browser availability of String.repeat here.

const repeatChars = (str, n = 2) => str.replace(/./g, v => v.repeat(n));
const repeatLetters = (str, n = 2) => str.replace(/[a-z]/gi, v => v.repeat(n));
const str = `abc d e`;
console.log(repeatChars(str));
console.log(repeatChars(str, 5));
console.log(repeatLetters(str, 2));

// when String.prototype.repeat is not available use:
function strRepeat(str, n = 2) {
  return [...Array(n)].map(_ => str).join(``);
}

You can simply use one of these two methods:

const doubleChar = (str) => str.split("").map(c => c + c).join("");

OR

function doubleChar(str) {
    var word = '';
  for (var i = 0; i < str.length; i++){
    word = word + str[i] + str[i];
  };
  return word;
};

function doubleChar(str) {
  
  let sum = [];
  
  for (let i = 0; i < str.length; i++){
    
   let result = (str[i]+str[i]);
   
   sum = sum + result;

  }
  
   return sum;

}

console.log (doubleChar ("Hello"));

发布评论

评论列表(0)

  1. 暂无评论