Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20659 > unrolled thread
| Started by | Saurabh Khemka <saurabh.khemka4@gmail.com> |
|---|---|
| First post | 2013-07-15 06:06 -0700 |
| Last post | 2013-07-16 16:18 +0100 |
| Articles | 9 — 9 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Saurabh Khemka <saurabh.khemka4@gmail.com> |
|---|---|
| Date | 2013-07-15 06:06 -0700 |
| Subject | Javascript insert |
| Message-ID | <a446ab6a-18bd-46af-8cd1-4b4f71867387@googlegroups.com> |
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. Regards, Saurabh
[toc] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2013-07-15 23:57 +1000 |
| Message-ID | <D4Cdnb9HV_lEYn7MnZ2dnUVZ_u-dnZ2d@westnet.com.au> |
| In reply to | #20659 |
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.
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.
Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-07-15 16:23 +0200 |
| Message-ID | <XnsA1FEA6CA9D7DBeejj99@194.109.133.133> |
| In reply to | #20661 |
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';
> 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');
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-07-15 16:41 +0100 |
| Message-ID | <0.c163817906fb839fe701.20130715164155BST.87wqorde4c.fsf@bsb.me.uk> |
| In reply to | #20662 |
"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)
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-07-15 16:17 +0000 |
| Message-ID | <ks17a7$4jo$4@dont-email.me> |
| In reply to | #20664 |
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
[toc] | [prev] | [next] | [standalone]
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Date | 2013-07-15 13:16 -0400 |
| Message-ID | <ks1aef$us4$1@dont-email.me> |
| In reply to | #20665 |
On 07/15/2013 12:17 PM, Denis McMahon wrote:
> 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');
>
How about:
a.replace("he","$1aaa");
--
Norman
Registered Linux user #461062
AMD64X2 6400+ Ubuntu 10.04 64bit
[toc] | [prev] | [next] | [standalone]
| From | JJ <duh@nah.meh> |
|---|---|
| Date | 2013-07-16 01:32 +0700 |
| Message-ID | <vf37b43zsbmd.bwr5w6ndbwff$.dlg@40tude.net> |
| In reply to | #20665 |
On Mon, 15 Jul 2013 16:17:11 +0000 (UTC), Denis McMahon wrote:
> 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');
Or...
String.prototype.insert = function(str, index) {
return this.substr(0, index) + str + this.substr(index);
};
//example
var s = "hello";
var r = s.insert("aaa", 2);
console.log(r);
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-07-15 21:15 +0200 |
| Message-ID | <5751004.8H4YKOi96x@PointedEars.de> |
| In reply to | #20659 |
Saurabh Khemka wrote: > I have a javascript variable say, var a = "hello";. There is neither “Javascript” nor “javascript”. <http://PointedEars.de/es-matrix> > I want to insert something after say, "aaa" so, that the final string > becomes > > "heaaallo". Please help. Yet another way, likely the one that can be adapted most easily: var m = a.match(/^(he)(llo)$/); var s = m[1] + "aaa" + m[2]; -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2013-07-16 16:18 +0100 |
| Message-ID | <b1pau8p9oqr1j8skmsol5jj4blpbqp4sto@4ax.com> |
| In reply to | #20668 |
On Mon, 15 Jul 2013 21:15:12 +0200, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: >Saurabh Khemka wrote: > >> I have a javascript variable say, var a = "hello";. > >There is neither “Javascript” nor “javascript”. If you don't know which languages he is referring to ... ><http://PointedEars.de/es-matrix> > >> I want to insert something after say, "aaa" so, that the final string >> becomes >> >> "heaaallo". Please help. >Yet another way, likely the one that can be adapted most easily: > > var m = a.match(/^(he)(llo)$/); > var s = m[1] + "aaa" + m[2]; ... how can you claim to give an an example of code in any of those languages ? John
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web