views:

61

answers:

1

I have a string like "My <color>"
I want to replace "<color>" with "Orange".
I did

str = str.replace("<color>","Orange");  

but it doesn't work.

How to do it?

+2  A: 

Answer to edited post:

So replace returns a copy of "replaced" string, it does not modify the original:

var string:String = "My <color>";
var replaced:String = string.replace("<color>", "Orange");
// My <color> My Orange
trace(string, replaced);

So you could do:

var str:String = "My <color>";
str = str.replace("<color>", "Orange");
// My Orange
trace(str);

Then str would be "My Orange"

Which is what your code says it does, but I think you didn't paste what you wrote or you have an error elsewhere in your program.


Answer to OP:

"" is an empty string, so you're basically saying "replace empty with Orange". A space is not empty. If you want "MyOrange", you'll want to use " " instead of "":

var str:String = "My ";
// MyOrange
trace(str.replace(" ", "Orange"));

If you want "My Orange" just append "Orange" to your string.

var str:String = "My ";
str += "Orange"
// My Orange
trace(str);

Can you provide some more input as to what your intended output should be so we can provide a more accurate answer?

Typeoneerror
His angle brackets were stripped off.. check the edited question.
Amarghosh