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

replace string literal

P: n/a
How would one make the ECMA-262 String.replace method work with a
string literal?

For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.

Please note my square brackets are not regular expression syntax.

Thanks,

Gary

Jun 6 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
gary wrote:
How would one make the ECMA-262 String.replace method work with a
string literal?

For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.

Please note my square brackets are not regular expression syntax.

Try

alert("hello".replace(/ll/i, 'jj') );

String literals are objects in JavaScript, unlike C.

--
Ian Collins.
Jun 6 '06 #2

P: n/a
Thank you very much for the reply, however, this does not solve my
problem.

For example

var h = "he[ll]o";
var regx = new RegExp ( h , 'i' );
alert(h.replace(regx, 'jj') );

Displays "he[ll]o" and not "he[jj]o" which is what I want.

Gary

Ian Collins wrote:
gary wrote:
How would one make the ECMA-262 String.replace method work with a
string literal?

For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.

Please note my square brackets are not regular expression syntax.

Try

alert("hello".replace(/ll/i, 'jj') );

String literals are objects in JavaScript, unlike C.

--
Ian Collins.


Jun 6 '06 #3

P: n/a
gary wrote:

Please don't top post!
Ian Collins wrote:
gary wrote:
How would one make the ECMA-262 String.replace method work with a
string literal?

For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.

Please note my square brackets are not regular expression syntax.


Try

alert("hello".replace(/ll/i, 'jj') );

String literals are objects in JavaScript, unlike C.

Thank you very much for the reply, however, this does not solve my
problem.

For example

var h = "he[ll]o";
var regx = new RegExp ( h , 'i' );
alert(h.replace(regx, 'jj') );

Displays "he[ll]o" and not "he[jj]o" which is what I want.

Which is exactly what my example does. What are you attempting to do?

--
Ian Collins.
Jun 6 '06 #4

P: n/a
Ian Collins wrote:
gary wrote:
How would one make the ECMA-262 String.replace method work
with a string literal?
<snip> Try

alert("hello".replace(/ll/i, 'jj') );

String literals are objects in JavaScript, unlike C.


No they are not. String primitive and string objects are distinct in
javascript, and string literals define string primitives.

The above works because the dot operator implicitly type-converts its
left hand side operand into an object, so the above is implicitly
equivalent to:-

alert((new String("hello")).replace(/ll/i, 'jj') );

Richard.
Jun 6 '06 #5

P: n/a
Richard Cornford wrote:
Ian Collins wrote:
alert("hello".replace(/ll/i, 'jj') );

String literals are objects in JavaScript, unlike C.


No they are not. String primitive and string objects are distinct in
javascript, and string literals define string primitives.

The above works because the dot operator implicitly type-converts its
left hand side operand into an object, so the above is implicitly
equivalent to:-

alert((new String("hello")).replace(/ll/i, 'jj') );


But perhaps you could clear something up for me. If I pass a string
object to a function (that may return an altered string), a reference
should be passed. So it would be more efficient, in general, to pass a
string object and have the function change the underlying string rather
than having to return the string, since returning means that the string
must be copied when there are no changes. My question is, how does one
change the string of the string object without bothering the reference?
I could always wrap the string in an array, but that doesn't seem like
it's in the right spirit.

Csaba Gabor from Vienna

In this non working example, I'd like the line in the function to
change the contents of the string object instead of changing the object
itself (which, as a result, won't be reflected in the caller).
var oStr = new String("example string")
function changeoStr (oStr) {
// possibly oStr.__parent__ or oStr.__proto__ could help?
// doesn't work. I'd just like the underlying string changed
oStr = "new string";
}
changeoStr(oStr);
alert (oStr); // I'd like the alert to show "new string"

Jun 6 '06 #6

P: n/a
Csaba Gabor wrote:
Richard Cornford wrote: <snip>
... . String primitive and string objects are distinct in
javascript, and string literals define string primitives.

<snip> ... . My question is, how does one change the string of the string
object without bothering the reference?

<snip>

Javascript does not have any means of changing the internal value of a
String object.

Richard.

Jun 6 '06 #7

P: n/a
Richard Cornford wrote:
Ian Collins wrote:
gary wrote:
How would one make the ECMA-262 String.replace method work
with a string literal?


<snip>
Try

alert("hello".replace(/ll/i, 'jj') );

String literals are objects in JavaScript, unlike C.

No they are not. String primitive and string objects are distinct in
javascript, and string literals define string primitives.

OK, thanks for pointing that out.

--
Ian Collins.
Jun 6 '06 #8

P: n/a
Richard Cornford wrote:
Csaba Gabor wrote:
Richard Cornford wrote:

<snip>
... . String primitive and string objects are distinct in
javascript, and string literals define string primitives.

<snip>
... . My question is, how does one change the string of the string
object without bothering the reference?

<snip>

Javascript does not have any means of changing the internal value of a
String object.


Thanks, I was afraid you might say something like that.
Well, if I can't unwrap the string object to get to the string, at
least I can wrap up the string in my own object to pretend that it's a
string object. Thus, I have a cleaner way to pass the string by
reference than wrapping it in an array. That is to say, other than a
distinct way to create it and a new in place replacement function
(.selfReplace), it appears to old code as a normal string.

I tried to modify the String object itself, similarly to the below (by
giving it a self value, to override any original value), but I didn't
get it working (yet?).

Csaba
function oString(str) {
this.__proto__ = new String(str);
this.toString = function() {return this.__proto__.toString(); };
this.valueOf = function() {return this.__proto__.valueOf(); };
this.selfReplace = function(needleRegExp, strReplacement) {
this.__proto__ = new String(this.replace(needleRegExp,
strReplacement)); } }
function changeStr (oStr, newStr) { oStr.selfReplace(/.*/, newStr); }

var oStr = new oString("my string"); alert("length: " + oStr.length);
oStr.selfReplace (/str/,"th"); alert("replaced string: " + oStr);
changeStr(oStr, "something new"); alert("changed string: " + oStr);

Jun 6 '06 #9

P: n/a
JRS: In article <11*********************@f6g2000cwb.googlegroups.c om>,
dated Mon, 5 Jun 2006 18:14:09 remote, seen in
news:comp.lang.javascript, gary <gb*****@gmail.com> posted :
How would one make the ECMA-262 String.replace method work with a
string literal?

For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.

Please note my square brackets are not regular expression syntax.


I assume that you want to use the above string as part of the RegExp, so
that a matching section can be found in a longer string.

S0 = "HELLO[WORLD]" // yours
S1 = S0.replace(/([\[\]])/g, "\\$1") // fix-up

T0 = "aaaHELLO[WORLD]cccxxxHELLO[WORLD]yyy" // test text

RE = new RegExp(S1)
T1 = T0.replace(RE, "bbb")

RE = new RegExp(S1, "g")
T2 = T0.replace(RE, "bbb")

A = [T1,,,T2] // results

No doubt you will need to fix up any other troublesome characters in S0,
by enhancing the line that gives S1.

Possibly, one could alternatively fix-up by converting characters to
Unicode???
*** DO NOT MULTI-POST ***
*** READ THE news:C.L.J 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.
Jun 6 '06 #10

P: n/a
Csaba Gabor wrote:
<snip>
I tried to modify the String object itself, similarly to the
below (by giving it a self value, to override any original
value), but I didn't get it working (yet?). <snip>
function oString(str) {
this.__proto__ = new String(str);
this.toString = function() {return this.__proto__.toString(); };
this.valueOf = function() {return this.__proto__.valueOf(); };
this.selfReplace = function(needleRegExp, strReplacement) {
this.__proto__ = new String(this.replace(needleRegExp,
strReplacement)); } }
function changeStr (oStr, newStr) { oStr.selfReplace(/.*/, newStr); }

<snip>

That seems a very extension dependent method of doing what ECMAScript
should be able to do by using a String object as the prototype for
another object:-

function VariableString(st){
this.set(st);
}
VariableString.prototype = new String('');
(VariableString.prototype.toString =
(VariableString.prototype.valueOf = function(){
return this.value;
})
);
VariableString.prototype.set = function(st){
this.value = String(st);
this.length = this.value.length;
};
function changeStr(oStr, newStr){
oStr.set(oStr.replace(/.*/, newStr));
}

var t = new VariableString('yyy')
alert(t);
changeStr(t, 'xxu xxz');
alert(
t+' '+
'\nindex of u [2] = '+t.indexOf('u')+
'\nlast index of x [5] = '+t.lastIndexOf('x')+
'\nsubstring(4, -1) [xxu ] = "'+t.substring(4, -1)+'"'+
'\ncharAt(2) [u ] = "'+t.charAt(2)+'"'+
'\ncharCodeAt(3) [32] = '+t.charCodeAt(3)+
'\nconcat(" ff", 4) [xxu xxz ff4] = '+t.concat(" ff", 4)+
'\nmatch("x") [x] = "'+t.match("x")+'"'+
'\nsearch("x") [0] = '+t.search("x")+
'\nslice(2, 5) [u x] = "'+t.slice(2, 5)+'"'+
'\nsplit("x") [,,u ,,z] = '+t.split("x")+
'\ntoUpperCase() [XXU XXZ] = "'+t.toUpperCase()+'"'+
'\nlength [7] = '+t.length
);

- Or using a closure to make the string value of the object private:-

function VariableString(st){
var value;
this.toString = this.valueOf = function(){
return value;
};
this.set = function(st){
value = String(st);
this.length = value.length;
};
this.set(st);
}
VariableString.prototype = new String('');

Richard.
Jun 7 '06 #11

P: n/a

Dr John Stockton wrote:
JRS: In article <11*********************@f6g2000cwb.googlegroups.c om>,
dated Mon, 5 Jun 2006 18:14:09 remote, seen in
news:comp.lang.javascript, gary <gb*****@gmail.com> posted :
How would one make the ECMA-262 String.replace method work with a
string literal?

For example, if my string was "HELLO[WORLD]" how would I make it work
in this instance.

Please note my square brackets are not regular expression syntax.


I assume that you want to use the above string as part of the RegExp, so
that a matching section can be found in a longer string.

S0 = "HELLO[WORLD]" // yours
S1 = S0.replace(/([\[\]])/g, "\\$1") // fix-up

T0 = "aaaHELLO[WORLD]cccxxxHELLO[WORLD]yyy" // test text

RE = new RegExp(S1)
T1 = T0.replace(RE, "bbb")

RE = new RegExp(S1, "g")
T2 = T0.replace(RE, "bbb")

A = [T1,,,T2] // results

No doubt you will need to fix up any other troublesome characters in S0,
by enhancing the line that gives S1.

Possibly, one could alternatively fix-up by converting characters to
Unicode???
*** DO NOT MULTI-POST ***
*** READ THE news:C.L.J 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.

Thanks John,

Yours seems to be the cloest answer yet.

It seems strange I have to handle things like square brackets myself.

The same would be the case for all regex special characters, so in
reality its not useable to any extent without writing a function which
handles everything.

Gary

Jun 7 '06 #12

P: n/a
Richard Cornford wrote:
Csaba Gabor wrote:
<snip>
I tried to modify the String object itself, similarly to the
below (by giving it a self value, to override any original
value), but I didn't get it working (yet?).

<example>
That seems a very extension dependent method of doing what ECMAScript
should be able to do by using a String object as the prototype for
another object:-
<example>
- Or using a closure to make the string value of the object private:-


....

Thanks for the nice examples. But is it not possible to modify the
String prototype directly? The below code that I tried works in FF,
but it fails in IE 6: changeStr works as expected, changing .self on
the string object. However, while the revised valueOf and toString
methods fire, they do not see (this).self, and I don't see what I'm
missing either.

Csaba

function protoRevamp (obj, strMethod) {
var tmpMethod=obj.prototype[strMethod];
obj.prototype[strMethod] = function(oldMethod) {
return function() {
if (this.self || this.self=="") return this.self;
return oldMethod.apply(this); } } (tmpMethod); }

protoRevamp (String, "valueOf");
protoRevamp (String, "toString");
String.prototype.selfReplace =
function(searchRE, strReplacement) {
this.self = this.replace(searchRE, strReplacement); }

function changeStr (oStr, newStr) { oStr.selfReplace(/.*/, newStr); }

var t = new String('yyy');
alert ("old t: " + t);
changeStr(t, 'xxu xxz');
alert(
'string [xxu xxz] = "' + t+'"'+
'\nself [xxu xxz] = "' + t.self+'"'+
'\nlength [7] = '+t.length+
'\nindex of u [2] = '+t.indexOf('u')+
'\nlast index of x [5] = '+t.lastIndexOf('x')+
'\nsubstring(4, -1) [xxu ] = "'+t.substring(4, -1)+'"'+
'\ncharAt(2) [u ] = "'+t.charAt(2)+'"'+
'\ncharCodeAt(3) [32] = '+t.charCodeAt(3)+
'\nconcat(" ff", 4) [xxu xxz ff4] = '+t.concat(" ff", 4)+
'\nmatch("x") [x] = "'+t.match("x")+'"'+
'\nsearch("x") [0] = '+t.search("x")+
'\nslice(2, 5) [u x] = "'+t.slice(2, 5)+'"'+
'\nsplit("x") [,,u ,,z] = '+t.split("x")+
'\ntoUpperCase() [XXU XXZ] = "'+t.toUpperCase()+'"'
);

Jun 7 '06 #13

P: n/a
Csaba Gabor wrote:
Richard Cornford wrote: <snip>
That seems a very extension dependent method of doing what
ECMAScript should be able to do by using a String object as
the prototype for another object:-


<example>
- Or using a closure to make the string value of the object private:-

...

Thanks for the nice examples. But is it not possible to modify the
String prototype directly?


Why? What you are proposing is in no way simpler or more obvious/clear
that my suggestions.
The below code that I tried works in FF,
but it fails in IE 6: ...
, and I don't see what I'm missing either.

<snip>

An appreciation of simplicity apparently.

Richard.

Jun 7 '06 #14

P: n/a
Richard Cornford wrote:
Csaba Gabor wrote:
Richard Cornford wrote: <snip>
That seems a very extension dependent method of doing what
ECMAScript should be able to do by using a String object as
the prototype for another object:-


<example>
- Or using a closure to make the string value of the object private:-

...

Thanks for the nice examples. But is it not possible to modify the
String prototype directly?


Why?


Why do I want to know? To further my understanding. I've done
almost nothing with prototypes, and it's evident that I don't have
a sufficient grasp of them yet. You'd be one of the first people
I'd expect to support furthering such understanding.
What you are proposing is in no way simpler or more obvious/clear
that my suggestions.


Nor did I even imply it.
The below code that I tried works in FF,
but it fails in IE 6: ...
, and I don't see what I'm missing either.

<snip>

An appreciation of simplicity apparently.


No, I have a deep appreciation of simplicity. However,
without understanding, appreciation tends to be shallow.
So to fully appreciate your solution, it makes sense for
me to understand why the approach I mentioned can't
work or what it would take to shore it up.

Csaba

Jun 7 '06 #15

P: n/a
Csaba Gabor wrote:
Richard Cornford wrote:
Csaba Gabor wrote: <snip>
... . But is it not possible to modify the
String prototype directly?
Why?


Why do I want to know?


No, why do you want to modify the String prototype directly?

<snip> ... . You'd be one of the first people
I'd expect to support furthering such understanding.

<snip>

And you would not have been someone I would have expected to champion
top-posting.

Richard.

Jun 7 '06 #16

P: n/a
Richard Cornford wrote:
Csaba Gabor wrote:
Richard Cornford wrote:
Csaba Gabor wrote: <snip> ... . But is it not possible to modify the
String prototype directly?

Why?
Why do I want to know?


No, why do you want to modify the String prototype directly?


For a small incremental modification of the way an object works, it
seems reasonable to me to modify the object definition. If I want to
have a repeat method on strings ("string".repeat(nTimes)) it seems
reasonable to me to modify String.prototype instead of creating a
separate wrapped object. Since the method wasn't there before, it
doesn't lead to a decrease in efficiency, and it won't disturb older
code.

While this argument doesn't hold for what I was after, it is still
incremental modification from a user's point of view: a string object
whose underlying string is changeable. So, it's not unreasonable to
consider altering the String prototype. Whether it actually makes
sense (and is doable) is a separate issue.

That was the original reason. However, at this point the reason is
simply understanding.

What I think fails in IE is that when you try to do something like
function protoRevamp (obj, strMethod) {
var tmpMethod=obj.prototype[strMethod];
obj.prototype[strMethod] = function(oldMethod) {
return function() {
if (this.self || this.self=="") return this.self;
return oldMethod.apply(this); } } (tmpMethod); }

protoRevamp (String, "valueOf");
protoRevamp (String, "toString");
var t = new String('yyy');
t.self = "foo";
alert (t);
instead of t.toString() or t.valueOf() being directly called to
accomodate the alert, IE seems to be making a new String object based
off of t, and then making the .toSring() call on the new object.
Frankly, I don't understand the logic here - seems more like the
department of redundancy department to create a new String object.
Anyway, the new object is not a copy of t because that .self property I
put on t isn't copied over to the new object (the .selfReplace method
is on the new object, since it's on String.prototype). Therefore,
this.self doesn't refer to t.self.

Seems to me like this approach is at a dead end here, and can't be
resurrected.
<snip>
... . You'd be one of the first people
I'd expect to support furthering such understanding.

<snip>

And you would not have been someone I would have expected to champion
top-posting.


Yea, that's the thing about expectations. People will be who they are
regardless of our expectations.

Jun 7 '06 #17

P: n/a
JRS: In article <11**********************@c74g2000cwc.googlegroups .com>
, dated Tue, 6 Jun 2006 21:50:01 remote, seen in
news:comp.lang.javascript, gary <gb*****@gmail.com> posted :

Dr John Stockton wrote:

*** READ THE news:C.L.J 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.
Jun 7 '06 #18

P: n/a
Thanks John, Yours seems to be the cloest answer yet. It seems strange I have to handle things like square brackets myself.
Why does that seem strange? Is it because your more familiar with VBs
replace function?

The replace method takes a RegExp so characters that have meaning in RegExp
will have to be escaped.
The same would be the case for all regex special characters, so in
reality its not useable to any extent without writing a function which
handles everything.


Add this in the top of your javascript:-

String.prototype.replaceText = function(search, replacement)
{
search = search.replace(this.rxSpecial,'\\$1')

var regexp = new RegExp(search, "g")

return this.replace(regexp, replacement)
}
String.prototype.rxSpecial =
/(\(|\)|\\|\^|\$|\||\?|\:|\[|\]|\.|\{|\}|\+|\*|\=|\!)/g

Now you can use this new method like this:-

"HELLO[WORLD]".replaceText("[WORLD]", " Kosmos")

Anthony.
Jun 8 '06 #19

P: n/a

Anthony Jones wrote:
Thanks John,

Yours seems to be the cloest answer yet.

It seems strange I have to handle things like square brackets myself.


Why does that seem strange? Is it because your more familiar with VBs
replace function?

The replace method takes a RegExp so characters that have meaning in RegExp
will have to be escaped.
The same would be the case for all regex special characters, so in
reality its not useable to any extent without writing a function which
handles everything.


Add this in the top of your javascript:-

String.prototype.replaceText = function(search, replacement)
{
search = search.replace(this.rxSpecial,'\\$1')

var regexp = new RegExp(search, "g")

return this.replace(regexp, replacement)
}
String.prototype.rxSpecial =
/(\(|\)|\\|\^|\$|\||\?|\:|\[|\]|\.|\{|\}|\+|\*|\=|\!)/g

Now you can use this new method like this:-

"HELLO[WORLD]".replaceText("[WORLD]", " Kosmos")

Anthony.


Thanks Anthony, that code was very useful.

Jun 8 '06 #20

P: n/a

"Anthony Jones" <An*@yadayadayada.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Thanks John,
Yours seems to be the cloest answer yet.

It seems strange I have to handle things like square brackets myself.


Why does that seem strange? Is it because your more familiar with VBs
replace function?

The replace method takes a RegExp so characters that have meaning in

RegExp will have to be escaped.
The same would be the case for all regex special characters, so in
reality its not useable to any extent without writing a function which
handles everything.


Add this in the top of your javascript:-

String.prototype.replaceText = function(search, replacement)
{
search = search.replace(this.rxSpecial,'\\$1')

var regexp = new RegExp(search, "g")

return this.replace(regexp, replacement)
}
String.prototype.rxSpecial =
/(\(|\)|\\|\^|\$|\||\?|\:|\[|\]|\.|\{|\}|\+|\*|\=|\!)/g

Now you can use this new method like this:-

"HELLO[WORLD]".replaceText("[WORLD]", " Kosmos")

Anthony.

Another thought I've had is that if you want to preform the same replacement
repeatedly then rather than calling replaceText so this is better:-

String.prototype.replaceText = function(search, replacement)
{
return this.replace(this.getRegExpForText(search), replacement)
}
String.prototype.getRegExpForText = function(search)
{
search = search.replace(this.rxSpecial,'\\$1')
return new RegExp(search, "g")
}
String.prototype.rxSpecial =
/(\(|\)|\\|\^|\$|\||\?|\:|\[|\]|\.|\{|\}|\+|\*|\=|\!)/g
Rather then calling replaceText in a loop call getRegExpForText before the
loop, keep the returned RegExp object in a variable and use it with the
native replace function in the loop.


Jun 8 '06 #21

P: n/a
Csaba Gabor wrote:
Richard Cornford wrote:
Csaba Gabor wrote:
<snip>
I tried to modify the String object itself, similarly to the
below (by giving it a self value, to override any original
value), but I didn't get it working (yet?).


<example>
That seems a very extension dependent method of doing what ECMAScript
should be able to do by using a String object as the prototype for
another object:-


<example>
- Or using a closure to make the string value of the object private:-

...
Thanks for the nice examples. But is it not possible to modify the
String prototype directly? The below code that I tried works in FF,
but it fails in IE 6: changeStr works as expected, changing .self on


In the beating a dead horse category, I took another approach. Rather
than trying to modify the String prototype directly (on account of IE
wanting to do a "copy" when it saw the string object), I thought
perhaps I could redefine it. And anyway the structure should be
cleaner. Once again, it works in FF and fails (though not so badly) in
IE.

But the failure in IE is still quite bad. Everything is OK except the
..length property is uniformly 0. That is to say, IE's jscript
steadfastly refuses to let me affix a .length property to the revised
String object. It wants to reflect the underlying prototype's .length,
as far as I can tell.
oldString = String;
String = function(st) { this.set(st); }
String.prototype = oldString.prototype;
String.prototype.toString = function() { return this.value; }
String.prototype.valueOf = function() { return this.value; }
String.prototype.set = function(replacement, searchRE) {
if (arguments.length<2) this.value = oldString(replacement);
else this.value = this.replace(searchRE, replacement); }

function changeStr (oStr, newStr) { oStr.set(newStr, /.*/); }

var u = new String('yyy');
var t = new String(u);
alert ("old t: " + t + "\nlength: " + t.length);
changeStr(t, 'xxu xxz');
alert(
'string [xxu xxz] = "' + t+'"'+
'\nlength [7] = '+t.length+
'\nindex of u [2] = '+t.indexOf('u')+
'\nlast index of x [5] = '+t.lastIndexOf('x')+
'\nsubstring(4, -1) [xxu ] = "'+t.substring(4, -1)+'"'+
'\ncharAt(2) [u] = "'+t.charAt(2)+'"'+
'\ncharCodeAt(3) [32] = '+t.charCodeAt(3)+
'\nconcat(" ff", 4) [xxu xxz ff4] = '+t.concat(" ff", 4)+
'\nmatch("x") [x] = "'+t.match("x")+'"'+
'\nsearch("x") [0] = '+t.search("x")+
'\nslice(2, 5) [u x] = "'+t.slice(2, 5)+'"'+
'\nsplit("x") [,,u ,,z] = '+t.split("x")+
'\ntoUpperCase() [XXU XXZ] = "'+t.toUpperCase()+'"'
);
alert ("old u: " + u + "\nlength: " + u.length);

Jun 9 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.