Mega Code Archive

 
Categories / Flash ActionScript / String
 

Use indexOf( ) in a while statement to get the indices of every occurrence of a substring

package{   import flash.display.Sprite;      public class Main extends Sprite{     public function Main(){         var example:String = "This string contains the word cool. Very cool. Yes, cool.";         var index:int =  -1;         while ( ( index = example.indexOf( "cool", index + 1 ) ) != -1 ) {                    trace( "String contains word cool at index " + index );         }     }   } }