A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] [MX04] String search & Replace with TextFormat??

  1. #1
    Senior Member
    Join Date
    Sep 2000
    Posts
    272

    [RESOLVED] [MX04] String search & Replace with TextFormat??

    Good morning all,

    I am attempting to simplify things further to speed development, so here goes.

    Below is a script I picked up sometime back, and it has served me well. However, I would now like to extend it by having the new text, in this case 'Betty Rubble.....' assigned the color 'blue' without having to manually identfy it's location in the string (7, 35, blue) as I am no doing by using the tip I picked up yesterday--it is the last line of the script below.

    Any ideas or assistance would be greatly appreciated.

    Code:

    String.prototype.searchReplace = function(find, replace) {
    return this.split(find).join(replace);
    };
    blue = new TextFormat();
    blue.color = 0x0000FF;
    var myString = "This is Fred Flinstone at the Bedrock Inn.";
    base.text = myString;
    trace(myString);
    btn.onRelease = function() {
    var newString = myString.searchReplace("Fred Flinstone", "Betty Rubble, Barney's wife,");
    trace(newString);
    base.text = newString;
    trace(newString);

    base.setTextFormat(7, 35, blue);
    };

  2. #2
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714
    Hi
    Maybe identify the strings that don't need replacing instead?
    Then whatever text you assign to the newString var will automatically be colored blue.
    code:
    String.prototype.searchReplace = function(find, replace) {
    return this.split(find).join(replace);
    };
    blue = new TextFormat();
    blue.color = 0x0000FF;
    var myString = "This is Fred Flinstone at the Bedrock Inn.";
    var firstKeptString = 7;
    var endKeptString = 19;
    base.text = myString;
    trace(myString);
    btn.onRelease = function() {
    var newString = myString.searchReplace("Fred Flinstone", "Betty Rubble, Barney's wife,");
    trace(newString.length);
    base.text = newString;
    trace(newString);
    base.setTextFormat(firstKeptString, newString.length-endKeptString, blue);
    };


  3. #3
    Senior Member
    Join Date
    Sep 2000
    Posts
    272

    Thanks

    Thanks Hum!

    Works great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center