A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] Delete characters in a string

  1. #1
    Senior Member
    Join Date
    Dec 2010
    Posts
    121

    resolved [RESOLVED] Delete characters in a string

    I'm trying to delete characters from a string with a for loop. I'm expecting a trace of 'Cats' but I'm keep getting 'Ctswsm'. I'm not sure what's going on. Any help would be appreciated

    Actionscript Code:
    var str1:String = "Cats are awesome";
                   
    for(var i:uint = 4; i < str1.length; i++)
    {
        str1 = deleteChar(str1, str1.charAt(i));
    }
    trace(str1);
    function deleteChar(string:String, char:String):String
    {
        return string.split(char).join("");
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    deleteChar deletes all instances of the character passed in, because split breaks the string into an array of strings delimited by the character given. Then it rejoins that array with no delimiters.

    So, following the execution of your program:
    str1 = "Cats are awesome".
    str1.charAt(4) = " ".
    deleteChar("Cats are awesome", " ")
    str1 = "Catsareawesome"
    str1.charAt(5) = "r"
    deleteChar("Catsareawesome", "r");
    str1 = "Catsaeawesome"
    str1.charAt(6) = "a"
    deleteChar("Catsaeawesome", "a")
    str1 = "Ctsewesome"
    str1.charAt(7) = "o"
    deleteChar("Ctsewesome", "o")
    str1 = "Ctsewesme"
    str1.charAt(8) = "e"
    deleteChar("Ctsewesme", "e")
    str1 = "Ctswsm"
    i = 9; i > str1.length.

  3. #3
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    Thanks. I figured that I need to change the value of i so that it starts at the correct position after a letter is deleted. And that works with 1 line text, but if I want to load multiple line text, at times, variable i doesn't subtract correctly for some reasons. I'm using Flash Builder to debug as well as to build the project, but I've also included the source folder with the text file I'm testing with.

    The miscalculated of i starts after "The world loves dog!". If you open the source file and play with it, you will see that i is calculated to the wrong value for some reason.

    Thanks.
    Attached Files Attached Files
    Last edited by flashmed; 04-14-2012 at 11:06 AM.

  4. #4
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    Found the error Suppposed to be i -= some value, and not i = some value

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You shouldn't use split to do this. Use substring or slice to get substrings of your string, then recombine them.
    Code:
    function deleteChars(from:String, where:int, howmany:int):String{
      var prefix:String = from.substring(0, where);
      var suffix:Strin = from.substring(where+howmany);
      return prefix+suffix;
    }

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