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, startIndex) + 1; } trace(count); } } }
Related Scripts with Example Source Code in same category :