A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [F8] Replacing character on a given position in a string

  1. #1
    Senior Member walnoot's Avatar
    Join Date
    Apr 2005
    Posts
    751

    [F8] Replacing character on a given position in a string

    Hi!

    Is there a way to replace a character in a string based on it's given index?

    Like:
    you have:
    "hello"

    and you want to change character at position 3 to "a"

    result:
    "healo"

    I'd rather don't want to change string into array first,
    modify array and turn it into string again.

    I hope there's a trick with slices, splits and/or joins ?
    Thanks in advance!

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    does this method work for your project ?
    PHP Code:
    function replaceChar(str,id,char){
    len str.length-1;
    preTxt str.substr(0,id-1); trace(preTxt); // he
    endTxt str.substr(id,len); trace(endTxt); // lo
    newTxt preTxt+char+endTxttrace(newTxt); // healo
    };

    myStr "hello";
    replaceChar(myStr,3,"a"

  3. #3
    Senior Member walnoot's Avatar
    Join Date
    Apr 2005
    Posts
    751
    Yes!
    Very nice,
    thank you.

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