How to add a "?" after every 2 characters?

Discussion in 'JavaScript' started by anilinkz, Aug 29, 2010.

  1. #1
    can someone help me with this,

    i have a string....
    
    var key='';
    key+='aaabacadag';
    
    Code (markup):
    i want to make it like this

    
    ?aa?ab?ac?ad?ag
    
    Code (markup):

    TIA.
     
    anilinkz, Aug 29, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Here is what you want:

    
    var key='';
    var str = 'aaabacadag';
    
    for(var i=0; i<str.length; i++){
        if(i%2==0){
            key += "?";
        }
    
        key += str.substr(i,1);
    }
    
    Code (JavaScript):
     
    s_ruben, Aug 30, 2010 IP
    anilinkz likes this.
  3. xcool101Man

    xcool101Man Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nice solution s_ruben
     
    xcool101Man, Aug 30, 2010 IP