new to javascript

G

groovyd

hi everyone, i have programmed quite extensively in C and C++ but am a
newbie to javascript and am currently busy working my way through
'JavaScript: The Definitive Guide', a great book infact.

A question i have is in the inability for java to mutate (change)
Strings, like string[3]='a'; kindof thing. Could someone please tell
me why? Seems like an important ability to be able to edit your
strings directly without forming another. atleast in C it was. maybe
i am too old school because it seems hard to teach this old dog new
tricks ;)

and speaking of hard to learn tricks... i am new to regex and am
trying to figure out an equivalent expression which would allow me to
replace a character of a string at a certain character position such
as string[3]='a'. i am assuming you do this sort of thing through the
regex replace() function?

/\w^\S*{3,5}(\n)+/... errrrrrrrrrrrr....

thanks in advance!
 
P

Peter Michaux

hi everyone, i have programmed quite extensively in C and C++ but am a
newbie to javascript

The newsgroup faq may be helpful

http://jibbering.com/faq/
and am currently busy working my way through
'JavaScript: The Definitive Guide', a great book infact.

I agree it is a good book for starting however it is frequently
referred to here as "the least bad book on JavaScript." There is a
reasonable amount of errata that is listed on the publisher's site. I
think Flanagan's experience with class-based languages has influenced
his use of JavaScript too much as JavaScript doesn't have classes but
he simulates them extensively. There are misleading bits of code
regarding browser scripting also and that may be the biggest problem.
A question i have is in the inability for java to mutate (change)
Strings, like string[3]='a'; kindof thing. Could someone please tell
me why? Seems like an important ability to be able to edit your
strings directly without forming another. atleast in C it was.

C is just a step above assembly and C is still primarily about
directly manipulating bits in RAM and on a disk. That's what makes C
good for that job. There is no abstraction between the code and the
hardware.

I haven't found myself yearning for mutable strings in JavaScript.
maybe
i am too old school because it seems hard to teach this old dog new
tricks ;)

and speaking of hard to learn tricks... i am new to regex and am
trying to figure out an equivalent expression which would allow me to
replace a character of a string at a certain character position such
as string[3]='a'. i am assuming you do this sort of thing through the
regex replace() function?

/\w^\S*{3,5}(\n)+/... errrrrrrrrrrrr....

string.replace(/^(.{3,3})(.)/, '$1a');

You could make a function to help with this

function replaceCharWith(str, pos, replacement) {
return str.replace(new RegExp('^(.{'+pos+','+pos+'})(.)'),
'$1'+replacement);
}

The following might be faster

function replaceCharWith(str, pos, replacement) {
return str.substring(0, pos) + replacement + str.substring(pos+1);
}


Peter
 
E

Evertjan.

Peter Michaux wrote on 26 dec 2007 in comp.lang.javascript:
and speaking of hard to learn tricks... i am new to regex and am
trying to figure out an equivalent expression which would allow me to
replace a character of a string at a certain character position such
as string[3]='a'. i am assuming you do this sort of thing through the
regex replace() function?

/\w^\S*{3,5}(\n)+/... errrrrrrrrrrrr....

string.replace(/^(.{3,3})(.)/, '$1a');

s = s.replace(/^(.{3})./, '$1a');

or

s = s.replace(/^(...)./, '$1a');
 
D

Dr J R Stockton

In comp.lang.javascript message <047e4ef5-2b45-4460-b6a7-c289702f5060@d2
1g2000prf.googlegroups.com>, Wed, 26 Dec 2007 05:57:52, groovyd
hi everyone, i have programmed quite extensively in C and C++ but am a
newbie to javascript and am currently busy working my way through
'JavaScript: The Definitive Guide', a great book infact.

You may find the pocket version also useful if the full one is just too
physically big at times, though I suspect there is no recent edition.
A question i have is in the inability for java to mutate (change)
Strings, like string[3]='a'; kindof thing. Could someone please tell
me why?

Java is not Javascript.

Maybe so that there can be multiple references to a string without
needing to worry about dealing for the need to change what one reference
sees.
Seems like an important ability to be able to edit your
strings directly without forming another. atleast in C it was.

It's never *necessary* to do exactly that, and one can write a
subroutine to implant a character in a given position in a copy of a
string. However, when frequently handling strings which are large or
numerous, do seek efficient plans with minimum bulk copying. It appears
that the implementation of array.join() is efficient.

and speaking of hard to learn tricks... i am new to regex and am
trying to figure out an equivalent expression which would allow me to
replace a character of a string at a certain character position such
as string[3]='a'. i am assuming you do this sort of thing through the
regex replace() function?

/\w^\S*{3,5}(\n)+/... errrrrrrrrrrrr....

Not necessarily. For S2 := S1 ; S2[3] := 'a' ;
you can do, for example
S2 = S1.substring(0, 2) + 'a' + S1.substring(3)
in which the indexes may need adjusting.

To do a lot, .split("") the string into an array, assign elements, and
..join("") .

I suspect that constructing and using a RegExp would not usually be
significantly faster - you can test that by taking a copy of <URL:http:/
/www.merlyn.demon.co.uk/js-quick.htm> and pressing Demo six times
without undue haste then inserting code. ... Using .substring seems
about twice as fast as using new RegExp.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
L

Lee

groovyd said:
hi everyone, i have programmed quite extensively in C and C++ but am a
newbie to javascript and am currently busy working my way through
'JavaScript: The Definitive Guide', a great book infact.

A question i have is in the inability for java to mutate (change)
Strings, like string[3]='a'; kindof thing. Could someone please tell
me why?

First, I'd like to point out that "new to javascript" is not
a good use of the Subject line. "Character replacement" is
one of many better choices.

Secondly, mutable strings are among the features that are
taken away in some modern languages to protect the programmer
from himself.

Problems include mangling the length of the string and the
confusion possible when there are multiple references to the
same string.


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top