By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

How do I modify a string?

P: n/a
I am trying to write a routine which follows this logic, but not sure
how to implement:

<script>
xxx = "... apple green yellow blue ...";

{right here I need to modify part of the string, so i.e. just 'green'
in the string above would become 'purple''}

alert(xxx);
</script>

How can I do this? Thank you!! :))

Aug 1 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
amyx...@gmail.com wrote:
I am trying to write a routine which follows this logic, but not sure
how to implement:

<script>
xxx = "... apple green yellow blue ...";

{right here I need to modify part of the string, so i.e. just 'green'
in the string above would become 'purple''}

alert(xxx);
</script>

How can I do this? Thank you!! :))
Check out regular expressions on the net or get a book reference. Very
valuable stuff.
xxx = xxx.replace( /green/, 'purple' );

for global replacement ( i.e. if xxx == 'apple green yellow blue green'
)
xxx = xxx.replace( /green/g, 'purple' );

for cas-insensitive replacement (i.e. xxx == 'apple Green yellow blue')
xxx = xxx.replace( /green/i, 'purple' )

-b

Aug 1 '06 #2

P: n/a

bobzimuta wrote:
amyx...@gmail.com wrote:
I am trying to write a routine which follows this logic, but not sure
how to implement:

<script>
xxx = "... apple green yellow blue ...";

{right here I need to modify part of the string, so i.e. just 'green'
in the string above would become 'purple''}

alert(xxx);
</script>

How can I do this? Thank you!! :))

Check out regular expressions on the net or get a book reference. Very
valuable stuff.
xxx = xxx.replace( /green/, 'purple' );

for global replacement ( i.e. if xxx == 'apple green yellow blue green'
)
xxx = xxx.replace( /green/g, 'purple' );

for cas-insensitive replacement (i.e. xxx == 'apple Green yellow blue')
xxx = xxx.replace( /green/i, 'purple' )

-b

Thank you for the complete and helpful response. Have a great upcoming
week! :) -A

Aug 1 '06 #3

P: n/a
Ray
bobzimuta wrote:
Check out regular expressions on the net or get a book reference. Very
valuable stuff.
xxx = xxx.replace( /green/, 'purple' );

for global replacement ( i.e. if xxx == 'apple green yellow blue green'
)
xxx = xxx.replace( /green/g, 'purple' );

for cas-insensitive replacement (i.e. xxx == 'apple Green yellow blue')
xxx = xxx.replace( /green/i, 'purple' )
Actually, do you really modify the string this way? String is immutable
in JavaScript isn't it? So it's not that the String is modified,
rather, the new String that is returned by the call to replace() is
assigned to xxx, is it?

Thanks!
Ray
>
-b
Aug 2 '06 #4

P: n/a
JRS: In article <11**********************@p79g2000cwp.googlegroups .com>
, dated Tue, 1 Aug 2006 11:54:23 remote, seen in
news:comp.lang.javascript, am*****@gmail.com posted :
>I am trying to write a routine which follows this logic, but not sure
how to implement:

<script>
xxx = "... apple green yellow blue ...";

{right here I need to modify part of the string, so i.e. just 'green'
in the string above would become 'purple''}

alert(xxx);
</script>

How can I do this? Thank you!! :))
Be aware that you cannot; a string cannot be modified. All that one can
do is to generate a new string and maybe assign it to the variable xxx,
which (if xxx is the only reference to the string) will free the
original for garbage collection. Generally, but not invariably, the
effects of unalterability are immaterial.

To dal with the contents of a string positionally, use such as .indexOf
and .substring; to deal more textually, including pattern-matching,
consider using a RegExp : see <URL:http://www.merlyn.demon.co.uk/js-
valid.htmand its links.

Read the newsgroup FAQ.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
Aug 2 '06 #5

P: n/a
Ray wrote on 02 aug 2006 in comp.lang.javascript:
bobzimuta wrote:
>Check out regular expressions on the net or get a book reference. Very
valuable stuff.
xxx = xxx.replace( /green/, 'purple' );

for global replacement ( i.e. if xxx == 'apple green yellow blue
green'
>)
xxx = xxx.replace( /green/g, 'purple' );

for cas-insensitive replacement (i.e. xxx == 'apple Green yellow
blue')
>xxx = xxx.replace( /green/i, 'purple' )

Actually, do you really modify the string this way? String is immutable
in JavaScript isn't it? So it's not that the String is modified,
rather, the new String that is returned by the call to replace() is
assigned to xxx, is it?
A string is not a defined entity in js, but is shorthand for several:

1 the content of a variable containing any number of characters.
2 a string litteral, a quoted part of js-code containing any number of
characters.

That 2 cannot be easily changed by the script is evident.

1 however can be, by overwriting the variable's content and keeping it in
a fresh memory space.

var s = "qwerty"

s = s + "asdfg"

Manipulating the content of the absolute memory location is only feasable
if the string memory location size is fixed, as used in assembler, but is
not efficient memorywize in higher languages, could have a
processorload/timing advantage. This is not possible in js.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 2 '06 #6

P: n/a
Ray
Evertjan. wrote:
Actually, do you really modify the string this way? String is immutable
in JavaScript isn't it? So it's not that the String is modified,
rather, the new String that is returned by the call to replace() is
assigned to xxx, is it?

A string is not a defined entity in js, but is shorthand for several:

1 the content of a variable containing any number of characters.
2 a string litteral, a quoted part of js-code containing any number of
characters.

That 2 cannot be easily changed by the script is evident.

1 however can be, by overwriting the variable's content and keeping it in
a fresh memory space.

var s = "qwerty"

s = s + "asdfg"
Ah, yes, but the overwriting here is actually just repointing s to
point to a new memory location that contains "qwertyasdfg", isn't it?
So it's not like you're modifying s in place, rather, you're making it
such that s points to a different string object?

I'm a Java programmer so my understanding is that JS string is similar
in its immutability to Java String, i.e.: you never do any in-place
modification to the string content itself (unlike say in C/C++).
Manipulating the content of the absolute memory location is only feasable
if the string memory location size is fixed, as used in assembler, but is
not efficient memorywize in higher languages, could have a
processorload/timing advantage. This is not possible in js.
Ah, yes. My conceptual model is correct then. Thanks! :)
>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 2 '06 #7

P: n/a
Dr John Stockton scribed:
>JRS: In article <11**********************@p79g2000cwp.googlegroups .com>
, dated Tue, 1 Aug 2006 11:54:23 remote, seen in
news:comp.lang.javascript, am*****@gmail.com posted :
>>I am trying to write a routine which follows this logic, but not sure
how to implement:

<script>
xxx = "... apple green yellow blue ...";

{right here I need to modify part of the string, so i.e. just 'green'
in the string above would become 'purple''}

alert(xxx);
</script>

How can I do this? Thank you!! :))

Be aware that you cannot; a string cannot be modified. All that one can
do is to generate a new string and maybe assign it to the variable xxx,
which (if xxx is the only reference to the string) will free the
original for garbage collection. Generally, but not invariably, the
effects of unalterability are immaterial.

To dal with the contents of a string positionally, use such as .indexOf
and .substring; to deal more textually, including pattern-matching,
consider using a RegExp : see <URL:http://www.merlyn.demon.co.uk/js-
valid.htmand its links.

Read the newsgroup FAQ.
<script....>
wordArray = [
'wordToUse1'
'wordToUse2'
..
..
'wordToUsen'
]

function whatWord(target,n) {
document.getElementById(target).innerHTML = wordArray[n];
}
</script>

<a href="#" onclick=whatWord('word1',1>Word 1</a>
etc.

string = '<span id=word1></span><span id = word2></span>...<span id
wordn></span>';

Obviously this may be embelished and refined, but is there something
incorrect with this approach? I hope not, as I use it [seemingly]
successfully.
--
Ed Jay (remove 'M' to respond by email)
Aug 2 '06 #8

P: n/a
Ray <ra********@yahoo.comwrote:
Actually, do you really modify the string this way? String is immutable
in JavaScript isn't it? So it's not that the String is modified,
rather, the new String that is returned by the call to replace() is
assigned to xxx, is it?
More or less. Strings in JavaScript are immutable.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 2 '06 #9

P: n/a
Christopher Benson-Manica wrote:
Ray wrote:
>Actually, do you really modify the string this way?
String is immutable in JavaScript isn't it? So it's
not that the String is modified, rather, the new
String that is returned by the call to replace()
is assigned to xxx, is it?

More or less. Strings in JavaScript are immutable.
It would be more accurate to state that there are no
operations/operators, built in functions or methods in javascript that
can modify the value of a string primitive or the internal [[Value]] of
a String object. When there is no mechanism for modifying a string the
mutability of the strings is irrelevant, they are effectively immutable
but the nature of the representation of the string primitive or String
object used internally by the javascript engine is not necessarily
restricted by the language.

Richard.
Aug 2 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.