Use indexOf in while loop to count the instance of a substring : IndexOf lastIndexOf : String Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » String » IndexOf lastIndexOf »

 

Use indexOf in while loop to count the instance of a substring


 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){


        var story:String = "It was a dark and stormy night...";
        var pattern:String = "a";
        var count:int 0;
        var startIndex:int 0;
        while (story.indexOf(pattern, startIndex!= -1) {
           count++;
           startIndex = story.indexOf(pattern, startIndex1;
        }
        trace(count);
    }
  }
}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo String
» IndexOf lastIndexOf