简体   繁体   中英

Actionsript/Flex String replace for <

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM