Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts

    JS: Insert a character at every 2nd character in a string

    I want to know if it's possible to insert a character at every 2nd character in a string. By that I mean like taking the string:

    1234567890

    and the character "a" and turning it into

    12a34a56a78a90

    Would that be possible? Help is greatly appreciated.

    BTW, I am a Javascript novice. :-)
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    var str = "1234567890";
    str = str.replace(/(\S{2})/g,"$1a");
    str = str.replace(/a$/,"");   // if you do not want the final a
    alert (str);
    </script>

    Quizmaster: Who was the Norwegian Explorer who first reached the South Pole?
    Contestant: Sherpa Tenzing
    Last edited by Philip M; 10-19-2008 at 04:21 PM.

  • Users who have thanked Philip M for this post:

    Jazzo (10-19-2008)

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Jazzo View Post
    I want to know if it's possible to insert a character at every 2nd character in a string. By that I mean like taking the string:

    1234567890

    and the character "a" and turning it into

    12a34a56a78a90

    Would that be possible?
    Yes it's certainly possible, for instance:alert("1234567890".replace(/(.{2})/g,"a$1"));, but if this is a homework question
    Code:
    12 Year Old Magician
    you'll have to conjure a more rudimentary method.

  • Users who have thanked Arty Effem for this post:

    Jazzo (10-19-2008)

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by Jazzo View Post
    I want to know if it's possible to insert a character at every 2nd character in a string.
    yes. its possible
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #5
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Thank you all very much
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Another way is to use a regular expression to match all the two character substrings.
    A match returns an array, so you can join it with the characters you want to insert.

    You'll need to take care of a trailing single character.

    string.length&#37;2 will tell you if the length is odd- leaves a remainder after dividing by 2.
    If so, you need to remember the final character, since it won't be returned in the array of two character matches.

    Code:
    var s= '11231234233440';
    var L= s.length;
    L=(L%2) ? 'a'+ s.charAt(L-1) : '';
    var M= s.match(/(.{2})/g) ;
    if(M) s= M.join('a')+ L;
    
    alert(s)
    Last edited by mrhoo; 10-19-2008 at 04:38 PM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Arty Effem View Post
    .....you'll have to conjure a more rudimentary method.

    Code:
    <script type = "text/javascript">
    var str = "1234567890";
    var newstr = "";
    var c;
    for (var i = 0; i<str.length; i++) {
    c = str.charAt(i);
    newstr = newstr + c;
    if (i%2 != 0 && (i<str.length-1)) {
    newstr = newstr + "a";
    }
    }
    alert (newstr);
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •