HighDots Forums  

How do I modify a string?

Javascript JavaScript language (comp.lang.javascript)


Discuss How do I modify a string? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
amyxmas@gmail.com
 
Posts: n/a

Default How do I modify a string? - 08-01-2006 , 02:54 PM






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!! )


Reply With Quote
  #2  
Old   
bobzimuta
 
Posts: n/a

Default Re: How do I modify a string? - 08-01-2006 , 03:02 PM






amyx... (AT) gmail (DOT) com wrote:
Quote:
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



Reply With Quote
  #3  
Old   
amyxmas@gmail.com
 
Posts: n/a

Default Re: How do I modify a string? - 08-01-2006 , 03:04 PM




bobzimuta wrote:
Quote:
amyx... (AT) gmail (DOT) 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



Reply With Quote
  #4  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: How do I modify a string? - 08-02-2006 , 09:01 AM



JRS: In article <1154458462.939815.289910 (AT) p79g2000cwp (DOT) googlegroups.com>
, dated Tue, 1 Aug 2006 11:54:23 remote, seen in
news:comp.lang.javascript, amyxmas (AT) gmail (DOT) com posted :
Quote:
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.htm> and 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #5  
Old   
Evertjan.
 
Posts: n/a

Default Re: How do I modify a string? - 08-02-2006 , 01:35 PM



Ray wrote on 02 aug 2006 in comp.lang.javascript:

Quote:
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)


Reply With Quote
  #6  
Old   
Christopher Benson-Manica
 
Posts: n/a

Default Re: How do I modify a string? - 08-02-2006 , 02:34 PM



Ray <ray_usenet (AT) yahoo (DOT) com> wrote:

Quote:
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.


Reply With Quote
  #7  
Old   
Ed Jay
 
Posts: n/a

Default Re: How do I modify a string? - 08-02-2006 , 02:57 PM



Dr John Stockton scribed:

Quote:
JRS: In article <1154458462.939815.289910 (AT) p79g2000cwp (DOT) googlegroups.com
, dated Tue, 1 Aug 2006 11:54:23 remote, seen in
news:comp.lang.javascript, amyxmas (AT) gmail (DOT) 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.htm> and 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)


Reply With Quote
  #8  
Old   
Richard Cornford
 
Posts: n/a

Default Re: How do I modify a string? - 08-02-2006 , 07:30 PM



Christopher Benson-Manica wrote:
Quote:
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.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.