Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20665

Re: Javascript insert

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Javascript insert
Date 2013-07-15 16:17 +0000
Organization A noiseless patient Spider
Message-ID <ks17a7$4jo$4@dont-email.me> (permalink)
References <a446ab6a-18bd-46af-8cd1-4b4f71867387@googlegroups.com> <D4Cdnb9HV_lEYn7MnZ2dnUVZ_u-dnZ2d@westnet.com.au> <XnsA1FEA6CA9D7DBeejj99@194.109.133.133> <0.c163817906fb839fe701.20130715164155BST.87wqorde4c.fsf@bsb.me.uk>

Show all headers | View raw


On Mon, 15 Jul 2013 16:41:55 +0100, Ben Bacarisse wrote:

> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
> 
>> Andrew Poulos wrote on 15 jul 2013 in comp.lang.javascript:
>>
>>> On 15/07/2013 11:06 PM, Saurabh Khemka wrote:
>>>> Hi,
>>>>
>>>> I have a javascript variable say, var a = "hello";.
>>>>
>>>> I want to insert something after say, "aaa" so, that the final string
>>>> becomes
>>>>
>>>> "heaaallo". Please help.
>>
>> You should define your Q better,
>> your example could be met with:
>>
>>    var a = 'hello';
>>    a = 'heaaallo';
> 
> This.  There's lot of ways to do it and the right best way is likely to
> depend on things not yet stated.
>>
>>> How about
>>>    var a = "hello";
>>>    a = a.split(""); a.splice(2, 0, "aaa");
>>>    a = a.join("");
>>> ?
>>> 
>>> It will fail if your original string includes a comma.
>>
>> It will NOT fail.
>>
>> ================================
>>
>> Shorter is:
>>
>>    var a = 'hello';
>>    a = a.replace(/(.{2})/, '$1aaa');
> 
> Longer, but probably conceptually simpler, and with fewer special cases
> to consider in the replacement string, is:
> 
>   a.substring(0, 2) + "aaa" + a.substring(2)

Purely in the interests of adding variety of solutions to the thread, I'd 
like to add:

String.prototype.insertAfterFirst = function(after,insert){ 
var there = this.indexOf(after);
return this.substring(0,there+1) + insert + this.substring(there+1);
}

var a = 'hello';
a = a.insertAfterFirst("e",'aaa');

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Javascript insert Saurabh Khemka <saurabh.khemka4@gmail.com> - 2013-07-15 06:06 -0700
  Re: Javascript insert Andrew Poulos <ap_prog@hotmail.com> - 2013-07-15 23:57 +1000
    Re: Javascript insert "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-07-15 16:23 +0200
      Re: Javascript insert Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-07-15 16:41 +0100
        Re: Javascript insert Denis McMahon <denismfmcmahon@gmail.com> - 2013-07-15 16:17 +0000
          Re: Javascript insert Norman Peelman <npeelman@cfl.rr.com> - 2013-07-15 13:16 -0400
          Re: Javascript insert JJ <duh@nah.meh> - 2013-07-16 01:32 +0700
  Re: Javascript insert Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-15 21:15 +0200
    Re: Javascript insert John Harris <niam@jghnorth.org.uk.invalid> - 2013-07-16 16:18 +0100

csiph-web