473,498 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new to javascript

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!
Dec 26 '07 #1
5 1256
groovyd said the following on 12/26/2007 8:57 AM:
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.
Does the book explain the difference between Java and Javascript?
A question i have is in the inability for java to mutate (change)
Strings, like string[3]='a'; kindof thing.
Huh? Sounds like you want to change one character of a string.

var myString = "My string";
myString = myString.split('');
myString[3] = "BOO";
myString = myString.join('');
alert(myString);

But no, it has no "built in" way to do what you are wanting to do.
Could someone please tell me why?
SWAG = "Nobody thought to put it in JS to do that".
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 ;)
The first thing you have to do is forget any, and all, notions of what
you think JS might be able to do based on what you can do in another
language. If you don't, it will drive you crazy.
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....
I am lucky to be able to spell Regular Expressions, much less write
them. So, I can't help you there.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 26 '07 #2
On Dec 26, 5:57 am, groovyd <goo...@groovydomain.comwrote:
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
Dec 26 '07 #3
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');

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 26 '07 #4
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.
--

Dec 26 '07 #5
In comp.lang.javascript message <047e4ef5-2b45-4460-b6a7-c289702f5060@d2
1g2000prf.googlegroups.com>, Wed, 26 Dec 2007 05:57:52, groovyd
<go****@groovydomain.composted:
>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.htmand 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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.